Search code examples
c++placement-new

How to determine if object has been placed using placement new


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?


Solution

  • 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:

    • Storing the info in the buffer.
      For this case the buffer has to be initialized at allocation. E.g. just add () 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:
      1. By adding room for a flag in the buffer.
      2. By having the flag as a member of the object.
        In the case of a polymorphic class there will in practice be a non-zero vtable pointer, which can serve as flag.
    • Storing the info externally.
      For this case the buffer does not need to be initialized at allocation. There are a zillion plus some possibilities, including
      1. A simple bool variable.
      2. A collection of pointers to all such objects.
      3. An audible signal emitted to a chimpanzee, which is later queried.