Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
On Sat, 12 Aug 2006 23:55:11 GMT, "Bill Reid"
<hormelfree@hap pyhealthy.netwr ote:
The point I was trying to make was: After the successful allocation,
you could dereference any address in the range address to
address+size-1. While it is legal to compute the value address+size
it is not legal to dereference it.
After calling realloc, any address based on the "before" location is
probably invalid. The only time it would be valid is if:
The address returned from realloc was the same as the address
passed to the function in argument 1 and
The offset into the area (address of interest - starting address
of area) <= size argument passed to realloc.
You asked why someone would want to dereference an address. I tried
to give an example of why it is a very common thing to do.
In my discussion, I used the phrase address+size in its non-C
arithmetic meaning. In C, the meaning is equivalent only for pointers
where the sizeof the object pointed to is 1.
In C address[size] is defined to be *(address+size) , remembering that
pointer arithmetic includes implied scaling by the sizeof the object
pointed to.
The "problem" is that dereferencing the address invokes undefined
behavior, even before you attempt to do something with the object that
may be retrieved from that address.
Remove del for email
On Sat, 12 Aug 2006 23:55:11 GMT, "Bill Reid"
<hormelfree@hap pyhealthy.netwr ote:
>
>Barry Schwarz <schwarzb@doezl .netwrote in message
>news:nm7qd2dur a0jcplvn9s0g5o4 73eb149mo3@4ax. com...
>OK, you were talking about dereferencing an address one element
>past the end of the block, I thought you were talking about something like
>saving the pointer, then trying to use it again after another realloc().
>That WOULD be a recipe for diasaster, right?
>Barry Schwarz <schwarzb@doezl .netwrote in message
>news:nm7qd2dur a0jcplvn9s0g5o4 73eb149mo3@4ax. com...
>On Fri, 11 Aug 2006 06:09:21 GMT, "Bill Reid"
><hormelfree@ha ppyhealthy.netw rote:
>>
>>
>It's a very common thing to do. How else do you get the value at that
>address? All subscripts involve an implied dereference.
>>
><hormelfree@ha ppyhealthy.netw rote:
>Barry Schwarz <schwarzb@doezl .netwrote in message
>news:k93od21vg d6n6fhrg6tooem3 r5j06ejrq4@4ax. com...
>On Fri, 11 Aug 2006 03:54:19 GMT, "Bill Reid"
><hormelfree@ha ppyhealthy.netw rote:
>news:k93od21vg d6n6fhrg6tooem3 r5j06ejrq4@4ax. com...
>On Fri, 11 Aug 2006 03:54:19 GMT, "Bill Reid"
><hormelfree@ha ppyhealthy.netw rote:
>You memory is
>allocated from address to address+size-1. Furthermore, calculating
>the value address+size is always allowed but you may not dereference
>this address.
>>
>...you wouldn't want to dereference an address, right.
>allocated from address to address+size-1. Furthermore, calculating
>the value address+size is always allowed but you may not dereference
>this address.
>>
>...you wouldn't want to dereference an address, right.
>It's a very common thing to do. How else do you get the value at that
>address? All subscripts involve an implied dereference.
>>
>past the end of the block, I thought you were talking about something like
>saving the pointer, then trying to use it again after another realloc().
>That WOULD be a recipe for diasaster, right?
you could dereference any address in the range address to
address+size-1. While it is legal to compute the value address+size
it is not legal to dereference it.
After calling realloc, any address based on the "before" location is
probably invalid. The only time it would be valid is if:
The address returned from realloc was the same as the address
passed to the function in argument 1 and
The offset into the area (address of interest - starting address
of area) <= size argument passed to realloc.
>
>So I'm not sure what distinction you're trying to make about
>subscript "implied" dereferencing. Isn't "address+si ze" equivalent to
>So I'm not sure what distinction you're trying to make about
>subscript "implied" dereferencing. Isn't "address+si ze" equivalent to
to give an example of why it is a very common thing to do.
>"address[size]"? Again, the only problem in doing anything with a
arithmetic meaning. In C, the meaning is equivalent only for pointers
where the sizeof the object pointed to is 1.
In C address[size] is defined to be *(address+size) , remembering that
pointer arithmetic includes implied scaling by the sizeof the object
pointed to.
>dereference of that address is that you're one element past the
>end of the block...but that might actually work for you if you're Russian...
>end of the block...but that might actually work for you if you're Russian...
behavior, even before you attempt to do something with the object that
may be retrieved from that address.
Remove del for email
Comment