Re: Undefined behavior or not?

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

 



On 2017/3/10 23:27, Bernd Edlinger wrote:
Hi,

I have a question regarding the following code:


char *p;
[...]
free(p);
if (p != NULL) {
   printf("the pointer was non-zero\n");
} else {
   printf("the pointer was null\n");
}


I would not have expected any problem with code like this,
which uses the pointer value after the free function was called,
as long as it does not dereference the pointer.


But in the N1570 annex J.2, the following is written:

"The behavior is undefined in the following circumstances:
...
The value of a pointer that refers to space deallocated by a call to the
free or realloc function is used (7.22.3)."

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. ...

--
Best regards,
LH_Mouse




[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