Do I need to delete an object that was created with a new and placement

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
}

Of course, I need to delete it if the removal Foohas some significant impact on the system, but it can be said that this is a simple storage object that I place, which is completely inside the buffer, and does not have a deconstructor that removes some other things.

  • Do I need to delete an object where there is space with a new one inside the buffer or is it enough to delete the buffer?
  • If I need to call delete on every object inside the buffer, why do I have this?

I read: what-uses-are-there-for-placement-new and it also says

You do not have to free all objects that use the memory buffer. Instead, you should remove [] only the source buffer.

+4
1

:

f-> ~Foo();

new . , , delete, new.

delete[] buffer;
+9

Source: https://habr.com/ru/post/1599861/


All Articles