Re: Just how delicate are freed pointers?
Jens.Toerring_at_physik.fu-berlin.de
Date: 05/16/04
- Next message: Alf: "Newbie confusion - when must I free the memory"
- Previous message: Leor Zolman: "Re: Just how delicate are freed pointers?"
- In reply to: Stephen L.: "Re: Just how delicate are freed pointers?"
- Next in thread: James Kanze: "Re: Just how delicate are freed pointers?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Alf: "Newbie confusion - when must I free the memory"
- Previous message: Leor Zolman: "Re: Just how delicate are freed pointers?"
- In reply to: Stephen L.: "Re: Just how delicate are freed pointers?"
- Next in thread: James Kanze: "Re: Just how delicate are freed pointers?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|