-
January 25th, 2011, 12:40 PM
#1
How to delete ptr from placement-new?
Hi Gurus,
I know, I must be a dork for not knowing it. But how do you delete a pointer created with placement-new? I'm trying to get with the sample below and I keep thinking that there should be a placement version of delete that correctly calls the dtor.
Is my code wrong?
Is there a right version of delete?
Thanks. Sincerely, Chris.
Code:
#include <memory>
#include <iostream>
typedef unsigned int UINT8;
class Something
{
public:
Something() { }
~Something()
{
std::cout << "Something's dtor." << std::endl;
}
};
void DoSomething(void)
{
// Get a local buffer.
UINT8 buffer[sizeof(Something)];
// Create ps in the local buffer.
Something* ps = new((void*) buffer) Something();
// Do something with ps.
// Delete ps.
ps->~Something();
}
int main(void)
{
DoSomething();
}
You're gonna go blind staring into that box all day.
-
January 25th, 2011, 12:43 PM
#2
Re: How to delete ptr from placement-new?
-
January 25th, 2011, 12:51 PM
#3
Re: How to delete ptr from placement-new?
Quicker than a blitz as usual, Paul.
Thanks.
You're gonna go blind staring into that box all day.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|