What is the right method to delete all the memory allocated here?
const char* charString = "Hello, World";
void *mem = ::operator new(sizeof(Buffer) + strlen(charString) + 1);
Buffer* buf = new(mem) Buffer(strlen(charString));
delete (char*)buf;
OR
const char* charString = "Hello, World";
void *mem = ::operator new(sizeof(Buffer) + strlen(charString) + 1);
Buffer* buf = new(mem) Buffer(strlen(charString));
delete buf;
or are they both same?
The correct method is:
You can only delete with the
deleteoperator what you received from thenewoperator. If you directly call theoperator newfunction, you must also directly call theoperator deletefunction, and must manually call the destructor as well.