delete pointer array

1
2
3
int * ptr = new int[5];
p += 2; //p now stores address of 3rd element in array
delete [] p;


what will be deleted??? all 5 elements or just 3rd, 4th and 5th elements of array(in result making a memory leak)
I assume ptr should be p.

Well, you've just created a whole set of problems. The question is, "How does delete [] p know how many elements to delete?"

That is implementation dependent, but, a common scheme is to store the number in the buffer itself. When you call delete [] p on an array that wasn't allocated with new [], the memory layout (type) won't match and you may delete less than you expect or call delete on elements beyond the range you're thinking about.

It's an error. Don't do it.

That's what's happening here: http://www.cplusplus.com/forum/beginner/140865
Last edited on
means if pointer is modified then delete won't work properly
means if pointer is modified then delete won't work properly

Yes, that's exactly what it means. You can only use delete with an address to the start of the memory that was originally allocated by new. Anything else results in undefined behaviour.
Topic archived. No new replies allowed.