Monday, January 16, 2006

C++ articles

here i will keep all interesting C++ articles:

article#1
--------------------------------------------------------------------------
C++: Under the Hood
Jan Gray
March 1994
http://msdn.microsoft.com/archive/default.asp?url=/archive/en-us/dnarvc/html/jangrayhood.asp
it talks about the C++ object model and layout in memory; good introduction;

article#2
--------------------------------------------------------------------------
here is a nice article about how vtables workits good coz it goes into asm language and explains whats really goin on
http://msdn.microsoft.com/msdnmag/issues/0300/c/
if u find any similar article on this subject, pls reply here.

article#3
--------------------------------------------------------------------------
virtual functions explained:
http://www.parashift.com/c++-faq-lite/virtual-functions.html

article#4
--------------------------------------------------------------------------
a good summerize of the classes size.
when applying the sizeof operator on an object you need to have in mind:

1Size of all non-static data members
2Order of data members
3Byte alignment or byte padding
4Size of its immediate base class
5The existence of virtual function(s) (Dynamic polymorphism using virtual functions).
6Compiler being used
7Mode of inheritance (virtual inheritance)

http://www.cprogramming.com/tutorial/size_of_class_object.html

article #5
--------------------------------------------------------------------------
very good C++ guidelines; yet, im not sure if all of them are applicable
http://www.doc.ic.ac.uk/lab/cplus/c++.rules/

Tuesday, January 10, 2006

how vtable work

here is a nice article about how vtables work
its good coz it goes into asm language and explains whats really goin on
http://msdn.microsoft.com/msdnmag/issues/0300/c/
if u find any similar article on this subject, pls reply here.

Wednesday, December 07, 2005

Problems

here I will put different little exercices I find.

6th grade problem:
find all integral numbers which after substraction to 9999 give the same number without the last digit. for example:
abcde - 9999 = abcd
damn, it must be easy

Saturday, December 03, 2005

SuspendThread

as the msdn says SuspendThread is for debugging purposes and should not be used.
it may be tempting to use it to pause a worker thread for example but it might not be ok.
the worker thread can be in the middle of executing a CRT function which is thread safe, using sychronization.
if the worker thread is paused after the lock is done, trying to execute from another thread a CRT function, which uses the same syncronization access, might get to a deadlock.

Thursday, November 10, 2005

stuff to do on DLL_PROCESS_ATTACH

it is guaranteed you can call any Kernel32.dll function on the DLL_PROCESS_ATTACH.
Calling any other function from other library is not ok, as the DLLs may not be loaded at that time (although your code links with those libs).
more info can be found in the J. Richter book. and J.Robbins say some problem he had in the past with some thread work in DLL_PROCESS_ATTACH.

using std lib functions between dlls

it is OK to have functions with variables of class vector or other std lib class, and call these functions between different DLLs executable modules or between EXE and DLLs, as long as both of the modules are linked in the same way to the C run time library (hence memory is alocated by the same code) .
info i think can be found in "C/C++ Run-Time Library Considerations", Chapter 6 in "Programming Apps for Windows" by J. Richter.

oversee CloseHandle

there are many WinAPI functions which create and return a HANDLE object, like CreateThread, CreateFile.
in windows header file, a HANDLE is defined like this
typedef void* HANDLE

doing a 'CloseHandle' more than it is needed on the same HANDLE value is as bad as doing 'delete' on the same pointer.
the docs say:
"CloseHandle invalidates the specified object handle, decrements the object's handle count, and performs object retention checks. After the last handle to an object is closed, the object is removed from the system."
so as long as the handle count is not zero, its logic of course to call CloseHandle.

there are few considerations with the HANDLE objects:
- some functions are not requiring to do CloseHandle on the their returned HANDLE object; so always, always read the docs; dont rely on the fact you are an experienced WinAPI programmer; the behaviour might be (it is !) different between functions (in fact you can find an example in the John Robbins book, Debugging Windows);
- doing a CloseHandle on an object which is invalid it may be disastrous;
Once, my CloseHandle hit a memory location where a CString was living;