SatView: Pointer Perfect, Part 2: Construction / Destruction
(Page 1 of 4 )
In this second article in our series about pointers, J. Nakamura explains malloc and free, and new and delete. He also examines the this pointer and discusses the difference between copying pointers and copying memory (and the hazards of shallow vs. deep copying). Then he points out the potential problems when crossing DLL boundaries, and finally takes us into the exotic(?) world of pointer pointers.
When you use the address dereference operator for pointer creation as mentioned in part 1, you are using pointers to objects that were created on the stack. More often you want to create objects on the heap (since the stack has a limited size), at which time you will have to use malloc/free or new/delete respectively.
The stack is that section of memory used for the storage of local variables and function parameters (among other things) and is used to keep track of function execution in your application. This means that upon leaving a function, all local variables created in that function will be thrown from the stack, i.e. destroyed.
You can use the heap when you need large chunks of data (e.g. large arrays of objects) that you don’t want to store on the stack to prevent it from overflowing. Or maybe you would like to keep the data alive across function calls or maybe you want to delay the construction of large objects.
Malloc & Free vs. New & Delete
malloc & free originate from C, while new & delete only work with a C++ compiler. Here lies their most important difference as well: malloc & free don’t understand constructors and destructors.
Lets take a look at the following two lines of code:
MyObj *pObj1 = static_cast<MyObj*>( malloc ( sizeof ( MyObj ) ) );
MyObj *pObj2 = new MyObj;
The first line creates a pointer to a chunk of memory that is large enough to hold MyObj, but there was no MyObj object constructed in that piece of memory. The second line creates a pointer to memory where MyObj has been constructed.
Now if you could somehow construct a MyObj object in the memory pObj1 points at, you would still have problems freeing that memory. Just as malloc doesn’t construct, free doesn’t destruct: you would end up with a memory leak.
When you are using C-functions, you might run into situations where you are given the responsibility to clean up chunks of memory (when you are using strdup for example). Be careful to match free with malloc and delete with new, because the results will be disastrous when you start mixing them!
Now take look at the following two lines of code:
MyObj *pObjArray = new MyObj[100];
/* some code here */
delete pObjArray;
What is wrong with this picture? Well, new MyObj[100] allocates enough memory for 100 MyObj objects and constructs them in that chunk of memory. The behavior of delete pObjArray is undefined, however. Most likely 99 of the 100 MyObj objects are not destructed! The proper way to delete allocated arrays on the heap is with the delete[] operator:
delete[] pObjArray;
Next: The This Pointer >>
More Code Examples Articles
More By J. Nakamura