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.