Sunday, June 17, 2012

new - delete Operators in C++

In our general communication language, if we observe something first time then we say it NEW.

Now consider a computer language, what is the basic thing needed by a program i.e. Memory. For running a program, we need memory for code instructions, we need memory for variables.
Now we have compile time memory and run-time memory allocations.

Code instructions will always be mapped to address locations at compile time only.
But when it comes to data variables, we can have compile time variables and run time variables.
Compiler will always maps the compile time variables with the addresses (Globals, Static etc.).
Run-time memory allocations happens in two ways:
1. Stack variables - happens implicitly and will be valid till the scope (local stack variables).
2. Heap variables - Programmer has to explicitly request for allocation and in the same way he has to explicitly request for deallocation from the heap (dynamic allocation).

Now come to the "new" keyword. In C++, Java etc, "new" keyword is used to allocate memory at run-time dynamically. The memory will be allocated in the heap section (similar to malloc / calloc in C).
Here our focus is on "new" operator in C++. So we will be discussing by considering c++ as our reference language.
new operator not only allocate memory at run time but does more then just allocation (compare to malloc / calloc). It invokes constructors, it returns the allocated address in appropriate Data Type format (we don't need to explicitly type cast the return address), we can overload new operator to provide placement new (managing preallocated buffer - placement new kind of functionality). We say new as a operator because C++ allows us to overload the operators and then we can overload new operator to have our own defined memory management.

Below is the examples of new operator:
-----------------------------------------------
Example [1]: normal new operator
class A{
...
...};


int main()
{
   A *a1 = new A(); //invokes the default constructor of class A and type casts the address to class A * type
   A *a2 = new A(10, 20); //invokes parameterised constructor of class A
   return 0;
}

Example [2]: placement new operator

char *buffer = new char[1000];
String *s1 = new(buf) String("First");
String *s2 = new(buf) String("Second");
------------------------------------------------


Now come to little bit in detail, when we say "new A()", the compile first invokes "operator new" (if defined - overloaded") and allocates the chunk of bytes then it invokes the constructor of class A and initializes the memory.

We can even allocate memory without calling the constructor as:

A *a1 = operator new(sizeof(A)); //this is similar to malloc call in C

----------------------------------------------------------------------------------------------------------

Now we know new allocates memory from the heap and will be reserved for the whole life of the program until we don't make call to deallocate the space (same as we make call to allocate). To deallocate the memory, we use delete operator. delete operator invokes the destructor of the class and then deallocates the object from the memory. In case of placement new, we need to overload delete (same - as we do for new operator) and then manage appropriately

---------------------------------------------------------------------------------------------------------

Please note that I assumed that the reader is having basic knowledge of C++ programming and programming concepts.

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Replies
    1. this articles are easy to understand while preserving the quality.

      Delete