Zero Sized Allocs - MaulingMonkey/ialloc GitHub Wiki

C, C++, and Rust all treat zero sized allocations differently.

Note that zero-sized types are common in Rust ((), [T; 0], etc.) whereas C and C++ mandate sizeof(T) >= 1, making zero-sized types much rarer in those languages. Additionally, Rust zero-sized allocations are typically represented as a non-unique dangling pointer, whereas C++ gives them unique (non-dereferenceable, but perhaps memory-consuming) non-null pointers (on success), whereas C might happily nullptr even for "success".

C

The order and contiguity of storage allocated by successive calls to the calloc, malloc, and realloc functions is unspecified. The pointer returned if the allocation succeeds is suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object or an array of such objects in the space allocated (until the space is explicitly deallocated). The lifetime of an allocated object extends from the allocation until the deallocation. Each such allocation shall yield a pointer to an object disjoint from any other object. The pointer returned points to the start (lowest byte address) of the allocated space. If the space cannot be allocated, a null pointer is returned. 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.

C Standard Draft ISO/IEC 9899:TC3 § 7.20.3 Memory Functions ¶ 1

C++

The allocation function attempts to allocate the requested amount of storage. If it is successful, it shall return the address of the start of a block of storage whose length in bytes shall be at least as large as the requested size. There are no constraints on the contents of the allocated storage on return from the allocation function. The order, contiguity, and initial value of storage allocated by successive calls to an allocation function is unspecified. The pointer returned shall be suitably aligned so that it can be converted to a pointer of any complete object type and then used to access the object or array in the storage allocated (until the storage is explicitly deallocated by a call to a corresponding deallocation function). Even if the size of the space requested is zero, the request can fail. If the request succeeds, the value returned shall be a nonnull pointer value (4.10) p0 different from any previously returned value p1, unless that value p1 was subsequently passed to an operator delete. The effect of dereferencing a pointer returned as a request for zero size is undefined. 32)


  1. The intent is to have operator new() implementable by calling malloc() or calloc(), so the rules are substantially the same. C++ differs from C in requiring a zero request to return a non-null pointer.

C++ Standard ISO/IEC 14882:2003 § 3.7.3.1 Allocation functions [basic.stc.dynamic.allocation] ¶ 2

⚠️ **GitHub.com Fallback** ⚠️