Search code examples
c++delete-operator

Can the argument of delete[] operator point to non-first element of an array?


The question is as above; if I have, for example, int* ptr = new int[10] and after some operations with data ptr is set to, say, 2nd, 3rd 5th or last element, will the call delete[] ptr be the correct one? Can't find it in the manual.


Solution

  • No, the argument passed to the delete[] operator must point to the same address that was returned by new[]

    (Assuming you haven't overloaded delete[] and new[] to do something crazy)