placement new and exception



 DEVELOP > c-Plus-Plus > placement new and exception

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "wijhierbeneden"
Date: 30 Oct 2004 08:24:00 AM
Object: 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???
.

User: "Pavel Vozenilek"

Title: Re: placement new and exception 30 Oct 2004 08:58:39 AM
"wijhierbeneden" wrote:

What exception can placement new throw???

Those thrown by constructor.
/Pavel
.
User: "wijhierbeneden"

Title: Re: placement new and exception 03 Nov 2004 02:46:14 AM
"Pavel Vozenilek" <pavel_vozenilek@yahoo.co.uk> wrote in message news:<2uhl0lF22nddtU1@uni-berlin.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.
Can you give an example about an exception???
.
User: "Richard Herring"

Title: Re: placement new and exception 03 Nov 2004 04:44:00 AM
In message <236f4e4.0411030046.709936eb@posting.google.com>,
wijhierbeneden <wijhierbeneden@hotmail.com> writes

"Pavel Vozenilek" <pavel_vozenilek@yahoo.co.uk> wrote in message
news:<2uhl0lF22nddtU1@uni-berlin.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.

Can you give an example about an exception???

--
Richard Herring
.
User: "Ron Natalie"

Title: Re: placement new and exception 03 Nov 2004 08:45:13 AM
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.
.
User: "Richard Herring"

Title: Re: placement new and exception 03 Nov 2004 09:18:49 AM
In message <4188ecd8$0$31343$9a6e19ea@news.newshosting.com>, Ron Natalie
<ron@sensor.com> writes

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
.





User: "JKop"

Title: Re: placement new and exception 30 Oct 2004 11:25:28 AM

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)] );
.
User: "Old Wolf"

Title: Re: placement new and exception 31 Oct 2004 01:49:33 PM
JKop <NULL@NULL.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++.
.
User: "JKop"

Title: Re: placement new and exception 02 Nov 2004 03:06:31 AM
Old Wolf posted:

JKop <NULL@NULL.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
.
User: "Bob Hairgrove"

Title: Re: placement new and exception 02 Nov 2004 04:17:45 PM
On Tue, 02 Nov 2004 09:06:31 GMT, JKop <NULL@NULL.NULL> wrote:

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
NoSpamPlease@Home.com
.



User: "David Lindauer"

Title: Re: placement new and exception 30 Oct 2004 03:24:16 PM
JKop 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)] );

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
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER