Connecting Tech Pros Worldwide Help | Site Map

Heap allocated object's class constructor throws an exception.

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 15th, 2006, 10:35 AM
Martin
Guest
 
Posts: n/a
Default Heap allocated object's class constructor throws an exception.

Hi.

I've found a few topics on this subject, but still not sure and decided
to post mine.

Here's my example:

#include <iostream>
using namespace std;

class A
{
public:
A() { throw 0; }
};

int main()
{
try
{
A *pA = new A;
delete pA;
}
catch (...)
{
}
}

An object of class A is created in the heap, but the constructor throws
an exception. I know, that the destructor of the class won't be called,
and really I don't need that, because my class itself doesn't allocates
any resources. But what happens with memory allocated for the object
itself? After all, the first thing done by 'new' is allocating enough
memory for the object. So, who's responsible for deallocating it in
such a case?

Thanks in advance

Martin


  #2  
Old November 15th, 2006, 10:45 AM
mlimber
Guest
 
Posts: n/a
Default Re: Heap allocated object's class constructor throws an exception.

Martin wrote:
Quote:
I've found a few topics on this subject, but still not sure and decided
to post mine.
>
Here's my example:
>
#include <iostream>
using namespace std;
>
class A
{
public:
A() { throw 0; }
};
>
int main()
{
try
{
A *pA = new A;
delete pA;
}
catch (...)
{
}
}
>
An object of class A is created in the heap, but the constructor throws
an exception. I know, that the destructor of the class won't be called,
and really I don't need that, because my class itself doesn't allocates
any resources. But what happens with memory allocated for the object
itself? After all, the first thing done by 'new' is allocating enough
memory for the object. So, who's responsible for deallocating it in
such a case?
The memory is automatically cleaned up. Buried in FAQ 11.14
(http://www.parashift.com/c++-faq-lit...tml#faq-11.14), you'll
find this snippet of code which is what happens when using new:

// This is functionally what happens with Foo* p = new Foo()

Foo* p;

// don't catch exceptions thrown by the allocator itself
void* raw = operator new(sizeof(Foo));

// catch any exceptions thrown by the ctor
try {
p = new(raw) Foo(); // call the ctor with raw as this
}
catch (...) {
// oops, ctor threw an exception
operator delete(raw);
throw; // rethrow the ctor's exception
}

As for A allocating its own memory before its constructor throws,
that's why you should use RAII techniques (such as smart pointers and
vectors) to control such memory. See
http://www.parashift.com/c++-faq-lit....html#faq-34.1.

Cheers! --M

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.