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

Data Handling in computers, though the ascii is called 7-bit code. why do w...

though the ascii is called 7-bit code. why do we use 8-bits to represent a character?

Files, redirection in c++

redirection in c++

Big M method, I Want a answer for solving the big M method in the topic of ...

I Want a answer for solving the big M method in the topic of simplex method...

#accept 3 digit number, Write a ''C'' program to accept any 3 digit integer...

Write a ''C'' program to accept any 3 digit integer number from the keyboard and display the word equivalent representation of the given number

C program to convert number to binary, Write a program in C that you will n...

Write a program in C that you will name "divide.exe", to divide one 32-bit twos-complement binary number by another, giving the quotient and the remainder, using the subtract-shift

Algorithm and flowchart, an algorithm and flowchart of finding the product ...

an algorithm and flowchart of finding the product of any two numbers

Define bitwise left shift and right shift operators, Define Bitwise Left Sh...

Define Bitwise Left Shift and Right Shift Operators: >? The bitwise shift operators shift their first operand right (>>) or left ( // Illustration of the bitwise right shi

COMPILER DESIGN, Compiler Design - Limit In The Method Instructions

Compiler Design - Limit In The Method Instructions

Miniumshelf, write a prgm to find minimum total number of shelves including...

write a prgm to find minimum total number of shelves including the intial one required for loading process

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