Skip to content

Standard Interop

MaulingMonkey edited this page May 1, 2023 · 1 revision

The Standards

C and C++ ensure even an empty structure has at least sizeof(T) >= 1, and gives them unique addresses short of edge cases like EBCO. Allocators generally treat 0 sized allocs as an unloved afterthought at best as a result. Additionally, sizeof(T) > alignof(T) is much rarer than in Rust, where ZSTs are common. Even the standard doesn't fully define behavior - only stating that it should return non-null on "success" (C doesn't even guarantee that much)

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

Of note here: A successful 17 byte allocation, assuming 17 >= sizeof(max_align_t), should still be aligned to alignof(max_align_t), even though 17 isn't going to be a multiple of anything but e.g. chars. This allows the somewhat common pattern of e.g. a 16-byte aligned header, followed by unaligned character data, without the caller having to do the work of rounding up their allocation request sizes. That said, C and C++ allocators should generally not be expected to return alignments greater than the sizes requested of them.

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

People be Funky

Clone this wiki locally