Monday, January 24, 2011

Different Types New And Delete Operation in C++...






Sample Code for the New and Delete Operations in C++:-

String *ps = new String();

causes compilers to generate code that approximately corresponds to this:

operator new(sizeof(ps)); // deallocate the memory
ps->string(); // call the object's ctor

First, it allocates enough memory to hold an object of the type requested. Second, it calls a constructor to initialize an object in the memory that was allocated. The new operator always does those two things; you can't change its behavior in any way.

The operator new function is usually declared like this:

void * operator new(size_t size);

The return type is void*, because this function allocates memory first before calling constructor and returns a pointer to raw, uninitialized memory. The size_t parameter specifies how much memory to allocate. You can overload operator new by adding additional parameters, but the first parameter must always be of type size_t.

Like malloc, operator new's only responsibility is to allocate memory. It knows nothing about constructors. All operator new understands is memory allocation. It is the job of the new operator to take the raw memory that operator new returns and transform it into an object.

Similarly, The memory deallocation is performed by the operator delete function, which is usually declared like this:

void operator delete(void *memoryToBeDeallocated);

That is,
String *ps;
…………..
delete ps;

causes compilers to generate code that approximately corresponds to this:

ps->~string(); // call the object's dtor
operator delete(ps); // deallocate the memory

Here destructor is called first and then memory is deallocated. So “Operator delete” also returns void * instead of X * where X is the class of which object is created. Like “New” and “Delete” Operator pair for allocating and deallocating of memory, “Operator new” and “Operator Delete” is used. Otherwise result will create the memory leak situation.

string *ps = new string[10]; // allocate an array of objects

The new being used is still the new operator, but because an array is being created, the new operator behaves slightly differently from the case of single-object creation. For one thing, memory is no longer allocated by operator new. Instead, it's allocated by the array-allocation equivalent, a function called operator new[] (often referred to as "array new.") Like operator new, operator new[] can be overloaded. This allows you to seize control of memory allocation for arrays in the same way you can control memory allocation for single objects. Similarly the paired operator to delete the memory allocation which is assigned during the “operator new[]” is “operator delete[]”. Here the call sequence of the constructor, destructor and memory allocation and deallocation is same as the operator new and delete.


Placement Syntax:-

The Solution for overloading the operator new with the extra argument to place the object in a particular address:-

Void * Operator new (size_t , void * p)
{
Return p; //place object at ‘p’
}

And can be invoked like this:-

Void * pf = (void *)0xFOOF;
X *px = new (pf)X; //Construct X at ‘buf’ invokes operator new(size,pf) that is the placement


So Below is the summary of the three different types of new and delete operations :-

1) Using “void * Operator new(size_t);” For Dynamic Memory Allocation of normal Object
“void * Operator delete(void * memLoc);” For Dynamic Memory deallocation of nomal Object


2) Using “void * Operator new[](size_t);” For Dynamic Memory Allocation of Array of Object
“void * Operator delete[](void * memLoc);” For Dynamic Memory deallocation of array Object

No comments:

Post a Comment