placement new and exception

W

wijhierbeneden

On http://www.parashift.com/c++-faq-lite/freestore-mgmt.html
in [16.9] In p = new Fred(), does the Fred memory "leak" if the Fred
constructor throws an exception?
there is this code:

// Original code: Fred* p = new Fred();
Fred* p = (Fred*) operator new(sizeof(Fred));
try {
new(p) Fred(); // Placement new
} catch (...) {
operator delete(p); // Deallocate the memory
throw; // Re-throw the exception
}

What exception can placement new throw???
 
J

JKop

Fred* p = (Fred*) operator new(sizeof(Fred));

I don't recognize the syntax above.

How about:

Fred* p = reinterpret_cast<Fred* const &>( new char[sizeof(Fred)] );
 
D

David Lindauer

JKop said:
Fred* p = (Fred*) operator new(sizeof(Fred));

I don't recognize the syntax above.

How about:

Fred* p = reinterpret_cast<Fred* const &>( new char[sizeof(Fred)] );

it is valid to take an overloaded operator function, and access it
directly. For example:

myclass &operator +(myclass &a, myclass &b)
{
}

....
....
myclass yy,zz ,ss;

ss = operator +(yy,zz) ;

is the same as:

ss = yy + zz ;

in rare circumstances this is useful...

David
 
O

Old Wolf

JKop said:
Fred* p = (Fred*) operator new(sizeof(Fred));

I don't recognize the syntax above.
How about:
Fred* p = reinterpret_cast<Fred* const &>( new char[sizeof(Fred)] );

JKop wrote on 27 Oct 2004 (4 days ago) in the thread
titled "Your C++ Homework":
If we throw the Standard Library out of the window for
the moment, then I would be comfortable saying here that
I'm an expert C++ programmer - I pretty much understand
and know how to use all of the features of C++.
 
J

JKop

Old Wolf posted:
JKop said:
Fred* p = (Fred*) operator new(sizeof(Fred));

I don't recognize the syntax above.
How about:
Fred* p = reinterpret_cast<Fred* const &>( new char[sizeof(Fred)] );

JKop wrote on 27 Oct 2004 (4 days ago) in the thread
titled "Your C++ Homework":
If we throw the Standard Library out of the window for
the moment, then I would be comfortable saying here that
I'm an expert C++ programmer - I pretty much understand
and know how to use all of the features of C++.


Am I the only one that finds this pathetic? Is it a lack of self-esteem
that motivates you to ridicule others?


-JKop
 
W

wijhierbeneden

Pavel Vozenilek said:
Those thrown by constructor.

/Pavel

And which ones??
bad_alloc can't be thrown because there must not been allocated any memory.
Can you give an example about an exception???
 
R

Richard Herring

In message said:
And which ones??
bad_alloc can't be thrown because there must not been allocated any memory.

Not necessarily. Placement new only suppresses the normal allocation for
the object itself. If the object has pointer members, its constructor
may attempt a normal allocation for whatever they point to, and that
allocation may throw.
 
R

Ron Natalie

Richard said:
Not necessarily. Placement new only suppresses the normal allocation for
the object itself. If the object has pointer members, its constructor
may attempt a normal allocation for whatever they point to, and that
allocation may throw.

....or the constructors class members.
 

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,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top