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

Assignment Help:

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().

 


Related Discussions:- In p = new fred(), does the fred memory "leak" if fred

How many non-boundary pits and peaks are on the map, Armed with your functi...

Armed with your function from above, we can do some interesting things. For instance, any pixel where the offsets are both zero is a pit (lower than all surrounding points) .

Sdsds, Given an integer n and a permutation of numbers 1, 2 ... , n-1, n wr...

Given an integer n and a permutation of numbers 1, 2 ... , n-1, n write a program to print the permutation that lexicographically precedes the given input permutation. If the given

Example of array, In this lab, please complete a given program to perform t...

In this lab, please complete a given program to perform the following tasks: 1. Allocate a 10 by 5 2D byte array dynamically. The way of allocation must be consistent with pag

When i develop a destructor, When I develop a destructor, do I require to e...

When I develop a destructor, do I require to explicitly call the destructors for my member objects?

Abstract class Employee , I have to add virtual void calculatePay and virtu...

I have to add virtual void calculatePay and virtual void displayEmployee. How to I implement that in Salaried and Hourly Employee?

Recursive function, Write a recursive function recursiveMin that takes an i...

Write a recursive function recursiveMin that takes an integer array, a starting subscript and an ending subscript as arguments, and returns the smallest element in the array. The

A Padovan string P(n) for a natural number n, c program for padovan string ...

c program for padovan string   Padovan series are positive integers obtained by the following process: The Padovan series is the sequence of integers P(n) defined by the

Differentiate between the expression ++a and a++, Differentiate between the...

Differentiate between the expression "++a" and "a++"? - With ++a, increment happens first on variable a, and resulting value is used. This is known as prefix increment. - Wi

How can dereferencing the pointer this, Dereferencing the Pointer this ...

Dereferencing the Pointer this Sometimes a member function requires to make a copy of the invoking instance so that it can change the copy without affecting the original instan

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd