| View previous topic :: View next topic |
| Author |
Message |
wijhierbeneden Guest
|
Posted: Sat Oct 30, 2004 1:24 pm Post subject: placement new and exception |
|
|
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???
|
|
| Back to top |
|
 |
Pavel Vozenilek Guest
|
Posted: Sat Oct 30, 2004 1:58 pm Post subject: Re: placement new and exception |
|
|
"wijhierbeneden" wrote:
| Quote: | What exception can placement new throw???
Those thrown by constructor. |
/Pavel
|
|
| Back to top |
|
 |
JKop Guest
|
Posted: Sat Oct 30, 2004 4:25 pm Post subject: Re: placement new and exception |
|
|
| Quote: | 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)] );
|
|
| Back to top |
|
 |
David Lindauer Guest
|
Posted: Sat Oct 30, 2004 8:24 pm Post subject: Re: placement new and exception |
|
|
JKop wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
Old Wolf Guest
|
Posted: Sun Oct 31, 2004 7:49 pm Post subject: Re: placement new and exception |
|
|
JKop <NULL (AT) NULL (DOT) NULL> wrote:
wiejherbeneden wrote:
| Quote: |
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++.
|
|
| Back to top |
|
 |
JKop Guest
|
Posted: Tue Nov 02, 2004 9:06 am Post subject: Re: placement new and exception |
|
|
Old Wolf posted:
| Quote: | JKop <NULL (AT) NULL (DOT) NULL> wrote:
wiejherbeneden wrote:
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
|
|
| Back to top |
|
 |
Bob Hairgrove Guest
|
Posted: Tue Nov 02, 2004 10:17 pm Post subject: Re: placement new and exception |
|
|
On Tue, 02 Nov 2004 09:06:31 GMT, JKop <NULL (AT) NULL (DOT) NULL> wrote:
| Quote: | Old Wolf posted:
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?
|
Probably.
--
Bob Hairgrove
[email]NoSpamPlease (AT) Home (DOT) com[/email]
|
|
| Back to top |
|
 |
wijhierbeneden Guest
|
Posted: Wed Nov 03, 2004 8:46 am Post subject: Re: placement new and exception |
|
|
"Pavel Vozenilek" <pavel_vozenilek (AT) yahoo (DOT) co.uk> wrote
| Quote: | "wijhierbeneden" wrote:
What exception can placement new throw???
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???
|
|
| Back to top |
|
 |
Richard Herring Guest
|
Posted: Wed Nov 03, 2004 10:44 am Post subject: Re: placement new and exception |
|
|
In message <236f4e4.0411030046.709936eb (AT) posting (DOT) google.com>,
wijhierbeneden <wijhierbeneden (AT) hotmail (DOT) com> writes
| Quote: | "Pavel Vozenilek" <pavel_vozenilek (AT) yahoo (DOT) co.uk> wrote in message
news:<2uhl0lF22nddtU1 (AT) uni-berlin (DOT) de>...
"wijhierbeneden" wrote:
What exception can placement new throw???
Those thrown by constructor.
/Pavel
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.
| Quote: | Can you give an example about an exception???
|
--
Richard Herring
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Wed Nov 03, 2004 2:45 pm Post subject: Re: placement new and exception |
|
|
Richard Herring wrote:
| Quote: | 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.
|
|
| Back to top |
|
 |
Richard Herring Guest
|
Posted: Wed Nov 03, 2004 3:18 pm Post subject: Re: placement new and exception |
|
|
In message <4188ecd8$0$31343$9a6e19ea (AT) news (DOT) newshosting.com>, Ron Natalie
<ron (AT) sensor (DOT) com> writes
| Quote: | Richard Herring wrote:
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.
|
Indeed, recursively, ad infinitum. I just picked the simplest case.
--
Richard Herring
|
|
| Back to top |
|
 |
|