Sign in to follow this  
Jethro_T

Pointer Arithmetic and Delete

Recommended Posts

Jethro_T    194
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?

Share this post


Link to post
Share on other sites
frob    44975
[quote name='mhagain' timestamp='1305729269' post='4812531']
Untested, but there's no reason why negative array indexes can't be used:
[code]delete[] &buffer[-20];[/code]
[/quote]

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.

Share this post


Link to post
Share on other sites
alvaro    21266
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.]

Share this post


Link to post
Share on other sites
EricRRichards    109
Why modify the buffer received from new, when you can just create a temporary pointer to access specific portions of the buffer (see below).[quote name='Jethro_T' timestamp='1305710518' post='4812391']
[source]
char* buffer = new char[64];

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


delete[] buffer;
[/source]


[/quote]

[size="2"]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.[/size]

Share this post


Link to post
Share on other sites
mhagain    13430
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

[quote]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.

Share this post


Link to post
Share on other sites
zacaj    667
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...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this