class Foo{
//some member
public:
int bar;
}
int main(){
char* buffer = new char[100];
Foo* f = new(buffer)Foo();
//do i have to
delete f;
//or is
delete[] buffer;
//enough
}
Sure i have to delete it if the delete of Foo
has some major effect on the system but lets say it is a simple storage object which i place which is compleatly inside of the buffer and has no deconstructor which does delete some other things.
I read: what-uses-are-there-for-placement-new and he also says
You should not deallocate every object that is using the memory buffer. Instead you should delete[] only the original buffer.
The correct way to destroy that object is with an explicit destructor call:
f-> ~Foo();
Usually placement new is used with memory on the stack. In this case, it's heap allocation, so you do need to free the buffer using the form of delete
that matches the new
.
delete[] buffer;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With