Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > using delete ex...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 5 Topic 45650 of 48020
Post > Topic >>

using delete expression on global operator new allocated memory

by dizzy <dizzy@[EMAIL PROTECTED] > Apr 30, 2008 at 01:07 PM

Hi

I wonder if this code is standard conformant and should work on all
conformant implementations (for some type T):

1: void* mem = ::operator new(sizeof(T));
2: T* p = new(mem) T(args...);
3: delete p;

line 2 I know it should be fine because global operator new should return
memory aligned for any type. The thing I wonder about is line 3. Should
this always work?

PS: the code might seem silly, it is needed because I need to decouple the
point of storage type used (which on 2 different codepaths can be on stack
or dynamic) from the point of actual initialization of the object and its
arguments (thus I need to use placement new); another solution I am aware
of whould be using some kind of "factory functors" (like the ones in
boost) 
but that would require for me to use template functions which is what I
want to avoid in the first place

-- 
Dizzy
 




 5 Posts in Topic:
using delete expression on global operator new allocated memory
dizzy <dizzy@[EMAIL PR  2008-04-30 13:07:33 
Re: using delete expression on global operator new allocated mem
Krice <paulkp@[EMAIL P  2008-04-30 06:57:49 
Re: using delete expression on global operator new allocated mem
"Alf P. Steinbach&qu  2008-04-30 16:02:19 
Re: using delete expression on global operator new allocated mem
James Kanze <james.kan  2008-05-02 01:09:28 
Re: using delete expression on global operator new allocated mem
dizzy <dizzy@[EMAIL PR  2008-05-02 12:22:30 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 8:58:08 CDT 2008.