4 ms·
Not exactly; from §7.20.3 in C99: If the size of the space requested is zero, the behavior is implementation- defined: either a null pointer is returne
by pbsd 12y ago
Not exactly; from §7.20.3 in C99:
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.
- AnimalMuppet 12y agoAh, my error. But, quoting from my ancient copy of The C++ Programming Language, volume 2, §r5.3.3: "This implies that an operator new() can be called with the argument zero. In this case, a pointer to an object is returned. Repeated such calls return pointers to distinct objects." So my memory was from C++ new, not C malloc.
- mcguire 12y ago"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" I love the C standards. Is that defining the behavior of malloc or the behavior of the code calling malloc?
- pbsd 12y agoThe caller code. It is saying that the caller cannot dereference the returned pointer, under pain of undefined behavior.
- AnimalMuppet 12y agoIn particular, you can't access (read or write) off either end of an allocated block. Well, if the block is of (nominal) length 0, there is nowhere to access safely.