new and memory management

C

Carlos Martinez

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
 
B

benben

Carlos said:
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
 
M

mlimber

Carlos said:
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-lite/dtors.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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top