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
How are postfix and prefix versions of operator++ () differentiated? A: The postfix version of operator++ () contain a dummy parameter of type int. The prefix version does not c

#include int main() { int T; int N; int i; scanf("%d",&T)


Define Storage Classes of c program - computer programming? Each variable and function in C language has two attributes that are type and storage class. If storage class of a v

develop a program read the following information from the keyboard in which base class consist of employee name code and destingnation the derived class contain the data members th

Bit-wise Operators Some applications require operations to be done on dissimilar bits of a byte separately. Bit-wise operators offer a facility to do just that. There are vario

New data members  New member functions  New constructors and destructor  New friends

Project Description: I want an experienced programmer to program an RFID tag that will be read (maybe also written), and perform a call to a Filmmaker Pro 13 database. Skills

Integer literal Integer is numbers without fractional parts. e.g. 20       // Decimal 024      // Octal     0x14     // Hexadecimal To indicate long, unsigned,

Project Description: I'm seeking someone who could write me a torrent seeding client which supports multiple proxies. The goal of the project is to make torrents popular by seed