Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Compiler's implementation of New and Delete operators

, 1 Feb 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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 fa
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 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 ; 
public:
 CellPhone(int ID, char* Name)
 {
 IMEI = ID ;
 strcpy(DeviceName,(char*)malloc(sizeof(strlen(Name)+1)));
 }
 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 excemption 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.

try
{
 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)
 {
 free(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 be clearly 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 demystify obscure implementation details of "New" and "Delete" operators. Suggestions and comments if any is appreciated.

License

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

Share

About the Author

Vijay Rajanna
Engineer
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 [^]
Follow on   Twitter   Google+

Comments and Discussions

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