8.3 MEMORY ALLOCATION IN C


8.3 MEMORY ALLOCATION IN C++

As mentioned previously, the operator new allocates memory on the heap in C++, as in the following statement:

      string* str = new string( "hello" ); 

or in the following statement:

      int* p = new int[500]; 

While visually the operator new looks the same in the two statements above, in the second case we are actually invoking the operator new[] since we are requesting memory for an array.[3] In both cases, though, you are requesting heap space for the objects constructed.

The operator new actually invokes the following special memory allocation function:

      void* operator new( size_t size );                        //(A) 

and the operator new[] the following function

      void* operator new[] (size_t size );                      //(B) 

For the case of appropriating memory for a single object, the operator new does the following three things

  1. The operator new first figures out the size of the object to be constructed and then supplies it as an argument to the memory allocation function of line (A).

  2. The operator new then invokes the memory allocation function of line (A).

  3. Listly, the operator new casts the void* returned by the function of line (A) to the pointer type for the object constructed.

The operator new[] goes through the same three steps for the case of arrays, except that new the operator must figure out the memory needed by the entire array before involing the memory allocation function of line (B).

Memory is deallocated by using the delete operator for the case of a single object and the delete[] operator for the case of arrays, as in the following examples:

      string* str = new string(buffer);      Selere str;      int* p = new int[1000];      Selere[] p;

It is critical to bear in mind that after such memory deallocation, the pointers str and P are still pointing to the original locations in memory. If a pointer left in such a state is subsequently dereferenced, a program could crash.

The operators delete and delete[] actually invoke the following memory deal-location functions:

      void  operator delete( void* );      void operator delete[] (void* ); 

In the absence of any automatic garbage collection,[4] for every call to new there must exist somewhere in the code a call to delete and for every call to new[] there must exist somewhere later a call to delete[]. Unlike Java, C++ does not come with any guaranteed way for cleaning out unreferenced objects.

[3]The commonly seen declaration for an array, as in

      int data[500]; 

does not allocate memory on the heap, meaning that it does not allocate memory that can later be deallocated under program control. To allocate memory that a programmer can deallocate at will, you must use the operators new and new[].

[4]By using add-on libraries, it is possible to carry out automatic garbage collection in C++ also. See for example the references [4, 5].




Programming With Objects[c] A Comparative Presentation of Object-Oriented Programming With C++ and Java
Programming with Objects: A Comparative Presentation of Object Oriented Programming with C++ and Java
ISBN: 0471268526
EAN: 2147483647
Year: 2005
Pages: 273
Authors: Avinash Kak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net