This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Undefined behavior or not?


* Andrew Makhorin:

>> No, that is not how it works.  6.2.4/2 says: [...] "The value of a
>> pointer becomes indeterminate when the object it points to (or just
>> past) reaches the end of its lifetime."
>> 
>> It does not matter whether the value of p changes in *your* system,
>> or in most systems.  Using the value of p after deallocating the object
>> it pointed to is undefined behaviour.
>> 
>
> Does this mean that the following fragment is invalid? (I think it
> isn't.)
>
>    void *p, *q;
>    p = malloc(123);
>    q = p;
>    free(p);
>    if (p == q) ...

Another common problem is the use of realloc with internal pointers,
adjusting them after the object has been moved.  There's no clean way
to write such code, except to avoid using pointers and use uintptr_t
everywhere.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]