Search code examples
c++memoryplacement-new

destructors of array placement new


I haven't been able to find an answer for this :

T * blockPtr = static_cast<T*>(malloc(nb*sizeof(T)));
new (blockPtr) T[nb];
// use my blockPtr array
// call destructors (?)
free(blockPtr);

In that case, what is the correct way to call destructors ? Should I manually loop on every item and call every destructor or is there a specific syntax to do this in one call ? I know that when calling delete[] on a class T, some compilers like MSVC usually have behind the scene a specific "vector destructor" to do this.


Solution

  • Should I manually loop on every item and call every destructor

    Yes.

    is there a specific syntax to do this in one call ?

    No.


    I hope you really need to do this!