Pointer Arithmetic and Delete

Started by
8 comments, last by zacaj 12 years, 11 months ago
I tried to google this since it seems like a straightforward question but I couldn't find any information.

[source]
char* buffer = new char[64];
buffer += 20;
delete[] buffer;
[/source]

Is it safe to do this?

Advertisement
No
It is legal yes... but it's generally not safe.

It is legal yes...
[/quote]
The pointer arithmetic is legal, but it is not legal to pass a value to delete [] that wasn't obtained from new [].
Untested, but there's no reason why negative array indexes can't be used:
delete[] &buffer[-20];

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.


Untested, but there's no reason why negative array indexes can't be used:
delete[] &buffer[-20];


My eyes burn.

Negative array indexes are illegal. Arrays are defined only from 0 to N-1.

There are template libraries that use a negative index to generate a compile-time assertion.


Don't do that.
I don't think negative array indexes are illegal. Negative-size arrays are, and that's what libraries might use for compile-time assertions.

Still, mhagain's code still calls delete[] on a pointer that was not obtained from new[], so I don't know what he thinks it achieves.
[EDIT: I see now what mhagain's code was doing. Yes, I believe it is correct, but EricRRichards's code below is much cleaner.]
Why modify the buffer received from new, when you can just create a temporary pointer to access specific portions of the buffer (see below).

[source]
char* buffer = new char[64];

char* iter = buffer;
iter += 20;
// do something useful with the iter pointer


delete[] buffer;
[/source]




Are you attempting to free just the portion of the buffer beyond the 20th byte? I'm a little rusty on the C++ way to do this, I've been doing so much C lately, but I don't think there is a direct analog to realloc(), so you'd probably have to new a new buffer, copy over the portion you wanted to retain, and delete the old one, in order to be safe.
Negative array indexes are perfectly legal.

http://users.ece.utexas.edu/~valvano/embed/chap8/chap8.htm#NEGATIVE
http://msdn.microsoft.com/en-us/library/59682zc4%28v=vs.80%29.aspx

Since a pointer may point to any element of an array, not just the first one, it follows that negative subscripts applied to pointers might well yield array references that are in bounds.[/quote]

True, they're a little shocking the first time you see them, but the core rule that the final reference is within the array bounds is still satisfied.

That doesn't mean that you should use them always (if nothing else they need a warning along the lines of "here's an exciting new way to shoot yourself in the foot" attached), but sometimes if they're the right tool for the job you want to do, then you shouldn't automatically discard the concept.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

Some compilers/OS's will figure that out, but others just do a search for the starting address when you do delete[], so you shouldnt do that...

This topic is closed to new replies.

Advertisement