By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,824 Members | 1,255 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,824 IT Pros & Developers. It's quick & easy.

Delete without calling destructor?

P: n/a

"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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.