Using the placement new syntax, I should be able to do something like this:
char *buffer = new char[sizeof(MyClass)]; //pre-allocated buffer
MyClass *my_class = new (buffer) MyClass; //put da class there
Now suppose I just do the first line, but not the second. Is there a way that it can be determined in code whether the buffer has been allocated appropriately, but no object of type MyClass has yet been instantiated there?
Original code:
char *buffer = new char[sizeof(MyClass)]; //pre-allocated buffer
MyClass *my_class = new (buffer) MyClass; //put da class there
To determine dynamically whether the placement new
has been performed information about it has to be stored somewhere. The language does not provide that service. So you have to do it yourself, in one of two possible main ways:
()
at the end of your new
expression. Otherwise there is no way to guarantee that the buffer contents don't look like an object. Two sub-cases:
bool
variable.