Re: using delete expression on global operator new allocated memory
* dizzy:
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
Yes, the code is silly.
Stack (or in C++ terminology local variable):
int main()
{
T o( args... );
}
Dynamic:
int main()
{
std::auto_ptr<T> p( new T( args... ) );
}
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?