468,513 Members | 1,785 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,513 developers. It's quick & easy.

Delete without calling destructor?


"delete" does two things:

1) Invokes the destructor
2) Deallocates the memory

We can manually invoke the destructor with:

p->~T();

But is there any way to manually deallocate the memory without invoking
the destructor? Something like "placement delete" perhaps? I'd be using
it where I'd be continuously constructing and destroying an object:
T *const p = new T;

p->~T();

::new(p) T;

p->~T();

::new(p) T;

p->~T();

::new(p) T;

p->~T();

::new(p) T;

p->~T();

SomeSortOfPlacementDelete(p); /* Just de-allocate, don't destruct */
--
Tomás Ó hÉilidhe
Feb 1 '08 #1
2 3274
On Feb 1, 2:21 pm, "Tomás Ó hÉilidhe" <t...@lavabit.comwrote:
"delete" does two things:

1) Invokes the destructor
2) Deallocates the memory

We can manually invoke the destructor with:

p->~T();

But is there any way to manually deallocate the memory without invoking
the destructor? Something like "placement delete" perhaps? I'd be using
it where I'd be continuously constructing and destroying an object:

T *const p = new T;

p->~T();

::new(p) T;

p->~T();

::new(p) T;

p->~T();

::new(p) T;

p->~T();

::new(p) T;

p->~T();

SomeSortOfPlacementDelete(p); /* Just de-allocate, don't destruct */
If you just need deallocate without calling the destructing, the
following is also a (probably sub-optimal) option, but it doesn't use
any explicit calls:

delete [] new char [sizeof(T)];

Feb 1 '08 #2
On Feb 1, 1:21 pm, "Tomás Ó hÉilidhe" <t...@lavabit.comwrote:
"delete" does two things:

1) Invokes the destructor
2) Deallocates the memory

We can manually invoke the destructor with:

p->~T();

But is there any way to manually deallocate the memory without invoking
the destructor?
free :)
Something like "placement delete" perhaps? I'd be using
it where I'd be continuously constructing and destroying an object:

T *const p = new T;
T *const p = static_cast<T *const>(malloc(sizeof(*p)));
::new(p) T;
>
p->~T();

::new(p) T;
etc.
SomeSortOfPlacementDelete(p); /* Just de-allocate, don't destruct */
free(p);

Ali
Feb 1 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

52 posts views Thread by Newsnet Customer | last post: by
14 posts views Thread by Timothy Madden | last post: by
20 posts views Thread by Ioannis Vranos | last post: by
1 post views Thread by Douglas Peterson | last post: by
10 posts views Thread by n2xssvv g02gfr12930 | last post: by
5 posts views Thread by junw2000 | last post: by
9 posts views Thread by rohits123 | last post: by
6 posts views Thread by mdinino | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.