AW: Undefined behavior or not?

onkel.jack@t-online.de onkel.jack@t-online.de
Sat Mar 11 15:39:00 GMT 2017


Dont mess up p and (*p).

Its true the the content of the memory p points to may change after free, depending on what other threads do and what the operating system is doing on free.
but the value of p, which contains the address of the allocated memory, does not change at free.
Its till holds that address.

This means, p stay as is but *p may change.

OJ


-----Original-Nachricht-----
Betreff: Re: Undefined behavior or not?
Datum: 2017-03-11T09:01:59+0100
Von: "Bernd Edlinger" <bernd.edlinger@hotmail.de>
An: "Martin Sebor" <msebor@gmail.com>, "Segher Boessenkool" <segher@kernel.crashing.org>, "Liu Hao" <lh_mouse@126.com>

On 03/10/17 23:25, Martin Sebor wrote:
> On 03/10/2017 09:12 AM, Segher Boessenkool wrote:
>> On Fri, Mar 10, 2017 at 11:57:28PM +0800, Liu Hao wrote:
>>>> So does this mean, that the above C code uses undefined behavior?
>>> Yes.
>>>
>>> 6.2.4 Storage durations of objects
>>> 1 ... Allocated storage is described in 7.22.3.
>>> 2 ... The value of a pointer becomes indeterminate when the object it
>>> points to (or just past) reaches the end of its lifetime.
>>>
>>> 7.22.3 Memory management functions
>>> 1 ... The lifetime of an allocated object extends from the allocation
>>> until the deallocation. ...
>>
>> But a null pointer does not point to an (allocated) object.  So the
>> code has undefined behaviour only if ever the pointer is not a null
>> pointer?
>
> That's right.  Same as in:
>
>     char *p = 0;
>
>     if (rand () & 1) {
>         char c;
>         p = &c;
>     }
>
>     if (p)
>       puts ("p i non-null");
>     else
>       puts (p is null");
>

Yes, addresses of automatic variables that do not escape
are clearly described in 6.2.4, my confusion was probably because
of the last sentence in paragraph 1, "Allocated storage is described
in 7.22.3." which made to unclear if the paragraph 2 applies to
allocated memory as well or if that is entirely described in 7.22.3.


Bernd.




More information about the Gcc-help mailing list