Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: C++
Expertise: All
Mar 29, 1999

Exceptions During Object Construction Don't Cause Memory Leaks

Operator new performs two operations: it allocates memory from the free store, and it constructs an object on the allocated memory. The allocated memory doesn't leak when an exception is thrown during the construction process. The memory is returned to the free store by the system before the exception propagates to the program. Thus, an invocation of operator new can be construed as two consecutive operations. The first operation merely allocates a sufficient memory block from the free store. If this operation is successful, the second one begins, which consists of invoking the object's constructor on the memory address retained in the previous step. If an exception occurs during this operation, the previously allocated memory is automatically released, and only then does the exception propagate to the next scope.
Danny Kalev
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap