In p = new fred(), does the fred memory "leak" if fred , C/C++ Programming

Q: In p = new Fred(), does the Fred memory "leak" if  Fred constructor throws an exception?

A: No.        

If an exception take place during the Fred constructor of p = new Fred(), the C++ language guarantees that the memory sizeof(Fred) bytes which were allocated will auto magically be released back to the heap.

Here the details: new Fred() is a two-step procedure:

sizeof(Fred) bytes of memory are allocated via the primitive void* operator new(size_t nbytes). This primitive is alike in spirit to malloc(size_t nbytes). (Note, though, that these two are not interchangeable; for example there is no guarantee that the two memory allocation primitives even employ the same heap!).

It develops an object in that memory by calling the Fred constructor. The pointer returned from the primary step is passed as the this parameter to the constructor. This step is wrapped out in a try catch block to manage the case while an exception is thrown during this step.

Therefore the real generated code is functionally similar to:

// Original code: Fred* p = new Fred();

Fred* p = (Fred*) operator new(sizeof(Fred));

try {

new(p) Fred(); // Placement new

}

catch (...) {

operator delete(p); // Deallocate memory throw; // Re-throw exception

}

The statement marked "Placement new" calls the Fred constructor. Inside the constructor the pointer p becomes the this pointer, Fred::Fred().

 

Posted Date: 3/19/2013 8:41:37 AM | Location : United States







Related Discussions:- In p = new fred(), does the fred memory "leak" if fred , Assignment Help, Ask Question on In p = new fred(), does the fred memory "leak" if fred , Get Answer, Expert's Help, In p = new fred(), does the fred memory "leak" if fred Discussions

Write discussion on In p = new fred(), does the fred memory "leak" if fred
Your posts are moderated
Related Questions
Project Description: I want a person who will be able to program a flex PCB for my device as i meet the person i will be able to describe all my needs Skills required are C P

brief note on multiple stack

Write a function for finding out highest and lowest marks obtained by a student from an array contained student name and marks. For Example : GetHighestLowestMarks{Student{n}, Ma

Write C code for a program that implements a digital clock (HH:MM:SS). The clock includes a two-digit hour indicator HH (00-23), a two-digit minute indicator MM (00-59), and a t


For this program you will add and test 2 new member functions to the IntSLList class posted on the website. The two member functions are: insertByPosn(int el, int pos) Assuming t

algorithm to find out all the factors of given positive integers

Damjibhai and Shamjibhai are two jeweler friends. They decide to play a simple game. The game comprises of removing the jewels for polishing, turn by turn. Once a jewel is removed

#decode the code

For this assignment, I've provided a data file called 'elevation.mat', which contains the elevation data you'll be working with.   Type load elevation.mat to get the varia