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
What is the purpose of return values? Can you not return any values from a function? If you could what would he function look like?

Write a Program to illustrate Array with Strings? main() { static char name[]="devdas"; int i; i=0; while(name[i]!='\0') { printf("%c",name[i]); i=i+1; } } In the

THIS PROGRAM IS TO DISPLAY A STOCK CONTAINING ITEM CODE,ITEM NAME,PRICE AND ANOTHER STOCK WITH CODE & QUANTITY AND DISPLAY COMPLETE INFORMATION #include #include #include

Define Enumeration in Computer Programming? Enumerated types enclose a list of constants that are able to be addressed in integer values. We can declare variables and types

fine the class bankAccount to implement the basic properties of a bank account. An object of this class should store the following data: Account holder''s name (string), account nu

Create a mathematical number guessing game. Have the user prompt for the number of games that they want to play. Then each game consists of the following rules. a. The computer

Question: a) Give two uses of NFS. b) Give one advantage of using NFS. c) Explain the following lines as they would be interpreted in the /etc/exports file i. /usr/local

Pointers are just numbers, representing addresses in memory. You can add to and subtract from pointers, for instance. It is pretty easy to make mistakes with pointer math, however,


Project Description: I want someone who can post ads for me on Craiglist . I will pay 3$ per ad i need about 30-40 ads per day . Skills required: C Programming, MySQL, Jav