By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,967 Members | 1,690 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,967 IT Pros & Developers. It's quick & easy.

new and memory management

P: n/a
Hi all:

When in a class constructor an exception is thrown, the class must free
memory it has reserved during initialization.
But what about the memory reserved for a class itself.

Ex:

MyClass* myObject;

try {
myObject = new MyClass();
} catch(excepcion& e) {
...
}

If the constructor of MyClass throws an exception, does new return
memory that must be freed?

Thanks in advance
Apr 20 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Carlos Martinez wrote:
When in a class constructor an exception is thrown, the class must free
memory it has reserved during initialization.
But what about the memory reserved for a class itself.


http://www.parashift.com/c++-faq-lit...html#faq-16.10

"In p = new Fred(), does the Fred memory "leak" if the Fred constructor
throws an exception?"

Always Google for the FAQ and such before posting!

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Apr 20 '06 #2

P: n/a
Carlos Martinez wrote:
Hi all:

When in a class constructor an exception is thrown, the class must free
memory it has reserved during initialization.
But what about the memory reserved for a class itself.

Ex:

MyClass* myObject;

try {
myObject = new MyClass();
} catch(excepcion& e) {
...
}

If the constructor of MyClass throws an exception, does new return
memory that must be freed?

Thanks in advance


The memory is freed so don't worry.

Regards,
Ben
Apr 20 '06 #3

P: n/a
Carlos Martinez wrote:
Hi all:

When in a class constructor an exception is thrown, the class must free
memory it has reserved during initialization.
But what about the memory reserved for a class itself.

Ex:

MyClass* myObject;

try {
myObject = new MyClass();
} catch(excepcion& e) {
...
}

If the constructor of MyClass throws an exception, does new return
memory that must be freed?

Thanks in advance


See also this FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-11.14

which describes in gory detail what happens implicitly when you use
new, including when an exception is thrown by your object's
constructor.

Cheers! --M

Apr 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.