The C11 standard added the aligned_alloc
function to allocate uninitialized aligned memory.
The standard also includes the calloc
function to allocate memory which is initialized to zero but only aligns it to the size of the largest type.
Why does C11 standard not include an aligned_calloc
function that allocate aligned memory that is zero initialized?
I am aware you can just memset the result to get initialized memory but the calloc
function is very useful on some operating systems as memory provided by the kernels must often already be zero initialized (and aligned to page size) for security reasons.
calloc
can make use of this and avoid double initialization.
Additionally some operating systems (like linux) provide memory in copy-on-write fashion that together with calloc
allows to construct primitive sparse data structures. An initializing memset on the result of aligned_alloc
destroys this property.
It seems to me that these advantages, while not portable, should be enough to have a second aligned allocation function.
The best guess I could offer is that an aligned_calloc specifically goes against one of the C1X charter's explicit goals:
Unlike for C9X, the consensus at the London meeting was that there should be no invention, without exception. Only those features that have a history and are in common use by a commercial implementation should be considered. Also there must be care to standardize these features in a way that would make the Standard and the commercial implementation compatible.
http://www.open-std.org/JTC1/SC22/wg14/www/docs/n1250.pdf
Looking around at commercial implementations, aligned_malloc was widely available and common to most every platform. An aligned calloc would have required more than wrapping on many platforms to offer more than the aligned_malloc() + memset() pair, thus could be considered inventive and thus was left out.
That'd be my best guess.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With