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?


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");

Martin


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