Array reassignment then deletion -- what happens to the orig array?

Say you have an array that you created using new.

buffer = new char[length]

where length is determined at runtime.

Now, say, for one reason or another you decide to reassign buffer so that it no longer points to the beginning of your array, but to another part of the array.

buffer = &buffer[i]

lets say i = 50.

Later, you decide to delete your array, like so

delete buffer[].

From the documentation, as I understand it, delete should delete all of our array that is pointed to by buffer. Does this include elements 1-50? It seems that since we reassigned buffer so that it points to the 50th element of our array, that there is no way delete is going to know about these original elements. It should only delete elements 50 to the end, no? What happens to our original 50 elements if they aren't deleted?

Thanks for your help

-Trent
you decide to reassign buffer so that it no longer points to the beginning of your array

Later, you decide to delete your array, like so

delete buffer[].

Undefined behavior occurs. Your program is terminated if you're lucky, the memory is silently corrupted if you're not.

You can only give delete[] a pointer that was returned by new[], not some other pointer 50 bytes to the right.
aahh very informative. Thanks Cubbi.
Topic archived. No new replies allowed.