Re: Just how delicate are freed pointers?

Jens.Toerring_at_physik.fu-berlin.de
Date: 05/16/04


Date: 16 May 2004 16:31:16 GMT

Stephen L. <sdlnospamar@cast-com.net> wrote:
> Malcolm wrote:
>>
>> "Stephen L." <sdlnospamar@cast-com.net> wrote in message
>> > The OP's original code is fine, although a
>> > little convoluted about the order of operations.
>> >
>> > The value of `a' will _not_ be changed by the
>> > compiler, the C standard, _any_ remotely conforming
>> > implementation of `free()' or the architecture
>> > (x86, Sparc, Power PC, etc.) the code is run under.
>> >
>> > You can do anything you want with the value of
>> > `a' with the following restrictions:
>> >
>> > 1. You cannot use it as a value to the `free()'
>> > function again,
>> > 2. You cannot de-reference it.
>> >
>> You should read a thread before responding.

> I did read the thread.

>> As others have pointed out, this is true for the vast majority of
>> implementations, but some will have trap values that trigger when an invalid
>> pointer (other than NULL) is used in a calculation. This means that
>> comparing a to b is illegal, as is incrementing a or doing anything else
>> with it.

> Sorry, the above is utter nonsense.

> Pointer arithmetic is always legal
> no matter what the state of the _value_
> of that pointer. This is a fact.

Sorry if you don't like it. But if you read the C standard carefully
you will find that after a pointer has been free()ed even _looking_
at its value (and not only dereferencing it) invokes undefined behavior.
We had that discussion I year ago here (actually I guess I was more
or less responsible for getting the thread drifting in that direction)
because I also couldn't imagine that just looking at the value of
a free()ed pointer could be forbidden. But I was proven wrong, of
course. Reading clc can tell you a lot about what you don't know,
even if you think you have seen it all (or at least most of it ;-).
It's the old saying: just because you will get away with it on most
architectures doesn't make it legal.

                                      Regards, Jens

-- 
  \   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
   \__________________________  http://www.toerring.de


Relevant Pages

  • Erradic/epileptic mouse behavior - Help!
    ... suddenly the mouse pointer is all over the screen. ... Like an epileptic system operator is in charge. ... arrow freezes and the mouse isn't responding. ... reboot I sometimes regain control of the mouse, ...
    (microsoft.public.windowsxp.hardware)
  • Un-controled Erratic/epileptic mouse movement/clicking
    ... suddenly the mouse pointer is all over the screen. ... Like an epileptic system operator is in charge. ... arrow freezes and the mouse isn't responding. ... reboot I sometimes regain control of the mouse, ...
    (microsoft.public.windowsxp.perform_maintain)
  • Re: opaque style question
    ... I was responding to Dan's conjecture ... FILE were an opaque type or not. ... reason for the Standard to say it mustn't be. ... *or* a [pointer to a] FILE object. ...
    (comp.lang.c)
  • Re: Exporting/importing a variable from a Dll
    ... Thanks for responding, your're right about the casting, so I cleaned that ... And in the dll I made a pointer to an integer, which allowed me to set it to ...
    (microsoft.public.vc.mfc)
  • Re: memset all bits to zero will let float/double to zero?
    ... could, and arguably should, use all-bits-zero for the null pointer. ... that handling a trap is relatively expensive. ... the data registers, not the address registers, so they'll work ...
    (comp.lang.c)