C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

placement new and exception

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
wijhierbeneden
Guest





PostPosted: Sat Oct 30, 2004 1:24 pm    Post subject: placement new and exception Reply with quote



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





PostPosted: Sat Oct 30, 2004 1:58 pm    Post subject: Re: placement new and exception Reply with quote




"wijhierbeneden" wrote:

Quote:
What exception can placement new throw???

Those thrown by constructor.


/Pavel



Back to top
JKop
Guest





PostPosted: Sat Oct 30, 2004 4:25 pm    Post subject: Re: placement new and exception Reply with quote




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





PostPosted: Sat Oct 30, 2004 8:24 pm    Post subject: Re: placement new and exception Reply with quote



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





PostPosted: Sun Oct 31, 2004 7:49 pm    Post subject: Re: placement new and exception Reply with quote

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





PostPosted: Tue Nov 02, 2004 9:06 am    Post subject: Re: placement new and exception Reply with quote

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





PostPosted: Tue Nov 02, 2004 10:17 pm    Post subject: Re: placement new and exception Reply with quote

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





PostPosted: Wed Nov 03, 2004 8:46 am    Post subject: Re: placement new and exception Reply with quote

"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





PostPosted: Wed Nov 03, 2004 10:44 am    Post subject: Re: placement new and exception Reply with quote

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





PostPosted: Wed Nov 03, 2004 2:45 pm    Post subject: Re: placement new and exception Reply with quote

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





PostPosted: Wed Nov 03, 2004 3:18 pm    Post subject: Re: placement new and exception Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.