c++ - Do i need to delete a object that was constructed with new and placement -


class foo{ //some member     public:     int bar; }  int main(){     char* buffer = new char[100];     foo* f = new(buffer)foo(); //do have     delete f; //or      delete[] buffer; //enough } 

sure have delete if delete of foo has major effect on system lets simple storage object place compleatly inside of buffer , has no deconstructor delete other things.

  • do have delete object places new inside of buffer or enough delete buffer?
  • if have call delete on every object inside of buffer, why have todo this?

i read: what-uses-are-there-for-placement-new , says

you should not deallocate every object using memory buffer. instead should delete[] original buffer.

the correct way destroy object explicit destructor call:

f-> ~foo(); 

usually placement new used memory on stack. in case, it's heap allocation, need free buffer using form of delete matches new.

delete[] buffer; 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -