Wiki Markup |
---|
TheWhen the resultsrequested ofsize allocatingis zero bytesthe behavior of the memory allocation usingfunctions {{malloc()}}, {{calloc()}}, orand {{realloc()}} areis [implementation-defined|BB. Definitions#implementation-defined behavior]. According to C99 Section 7.20.3 \[[ISO/IEC 9899:1999|AA. References#ISO/IEC 9899-1999]\]: |
If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object.
In addition, the amount of storage allocated by a successful call to the allocation function when 0 bytes was requested is unspecified. See unspecified behavior 40 in section J.1 of the standard.
In cases where the memory allocation functions return a non-null pointer, using this pointer reading from or writing to the allocated memory area results in undefined behavior. Typically the pointer refers to a zero-length block of memory consisting entirely of control structures. Overwriting these control structures will damage the data structures used by the memory.
Noncompliant Code Example (malloc()
)
...