Re: Undefined behavior or not?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.




[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux