Click here to Skip to main content
12,996,709 members (78,809 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


1 bookmarked
Posted 1 Feb 2012

Compiler's Implementation of New and Delete operators

, 1 Feb 2012
Rate this:
Please Sign up or sign in to vote.
Compiler's implementation of new and delete operators

Digging deeper into the implementation of New and Delete operators, raises couple of question as listed below:

  • Under what circumstances a bad_alloc exception is thrown?
  • What if there is an exception thrown inside a constructor?
  • Who releases the heap memory allocated for the object, when constructor fails?
  • Is it possible to reuse a chunk of memory on the heap to reconstruct the object?
  • How to distinguish whether the exception is due to heap allocation error or due to a failing constructor?

Rather than trying to answer these questions individually, it's better to explore compiler implementation of " new " and " delete " operators which eventually answers the above questions.

Consider CellPhone, A Class Declaration Below

class CellPhone
 long IMEI ;
 char* DeviceName ; 
 CellPhone(int ID, char* Name)
 IMEI = ID ;
 void* operator new ( size_t size );

void operator delete (void* buffer);

The below instruction creates an instance of this class on the heap through " new " operator.

CellPhone* PhoneObject = new CellPhone(900,"NOKIALUMIA") ;

New Operator

The "new" operator instantiates an object on the heap in two steps:

  1. Allocate the required amount of memory for the object, on heap
  2. Call constructor of the class to initialize the object

Instantiating the Object on Heap

To allocate memory on the heap "new" operator uses a function " operator new " as shown in the class declaration. This function allocates memory specified by " size_t " parameter on heap for the object and returns a " void* " pointer. And if the memory allocation fails, run-time will throw a " bad_alloc " exception.

void* CellPhone::operator new(size_t size) throw (const char*)
void* buffer = malloc(size);    
if(buffer = NULL) throw "Not enough memory to allocate for the object";
return buffer ;

The above function can be overloaded by the developer to provide custom implementations and exemption details.

Initializing the Object by Calling Constructor

Run-time achieves this functionality through " Placement new " operator which basically receives " void* " pointer returned by " operator new " function, allocated on the heap and calls constructor of the class to initialize the object as shown below.

Calling operator new function.

void* buffer = operator new(sizeof(CellPhone)); // ----------->1

Typecast void* pointer to class specific pointer.
 //Simple static cast also would do.
 CellPhone* PhoneObject = dynamic_cast<CellPhon*>(buffer);

Constructing the object at the allocated space using " placement new " operator.

 new(PhoneObject)CellPhone(900,"NOKIA LUMIA"); //------------> 2
 catch(exception& e)
 cout << e.what();

 operator delete( PhoneObject );

throw "Exception inside constructor"; 

void CellPhon::operator delete (void* buffer)
 buffer = NULL ; 

The above programming structure clearly explains how the exceptions from constructor is handled. The " placement new " operator shown in instruction #2 receives pointer to the buffer allocated as shown in instruction #1 and tries to construct the object at that location.

During this process, if there is any exception thrown inside the constructor, it is caught and the allocated memory is released back to heap using " operator delete ( ) " function, which is a compliment of " operator new ( ) " function.

With the above implementation, user would clearly be able to distinguish whether the " new " operator failed due to "heap allocation error " or due to " exception thrown " inside the constructor.

I hope this article was helpful in demystifying the obscure implementation details of "New" and "Delete" operators. Suggestions and comments if any are appreciated.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Vijay Rajanna
India India
I am Vijay hailing from Bangalore, the silicon valley of India. I posses extensive expertise working in mobility domain. I have a natural propensity for "Research and Innovation".

My areas of interest include Pervasive computing, Cognitive sensors, HCI, and Multimedia. Being strongly resolute with an everlasting fervor for learning, over the years I have learnt to come out of comfort zone to experiment with state of the art technologies and to contribute to developer community.

I blog about innovations and latest technologies from mobility domain, about development environments and programming languages across different platforms.

Blog : Amusement of a speculative mind...[^]
Projects : Amusement of a dilettante mind...[^]
LinkedIn : Vijay D R [^]

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170609.2 | Last Updated 1 Feb 2012
Article Copyright 2012 by Vijay Rajanna
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid