Undefined behavior or not?

Bernd Edlinger bernd.edlinger@hotmail.de
Sun Mar 12 08:43:00 GMT 2017


On 03/12/17 02:14, Andrew Makhorin wrote:
>> 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) ...
>
> Thanks.
>

Yes, as well as this:

   char *p, *q;
   p = malloc(123);
   q = p + 123;
   free(p);
   if (q == p + 123) ...


Bernd.


More information about the Gcc-help mailing list