Frage

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.

War es hilfreich?

Lösung

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)

Andere Tipps

From the C++ reference manual delete[]: Notice that an invalid value of ptr causes undefined behaviour.
Because of this it is not recommended to manipulate the pointer to your allocated memory directly!

Can't find it in the manual

C++11 3.7.4.2/3 says

behavior is undefined if the value supplied to operator delete[](void*) in the standard library is not one of the values returned by a previous invocation of either operator new[](std::size_t) or operator new[](std::size_t, const std::nothrow_t&) in the standard library

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top