18

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.

Peter Cordes
  • 245,674
  • 35
  • 423
  • 606
jtaylor
  • 2,079
  • 13
  • 18
  • 2
    Probably because design is the art of deciding what to include and what to leave out, and this didn't meet the bar (even though many functions of questionable use did). Anyway, there's nothing stopping any implementation from providing it and making inclusion in the next standard more likely. – Deduplicator Apr 15 '14 at 19:32
  • Not only any system is free to provide an `aligned_calloc` but you can probably already emulate the functionality on many systems by mmap()ing /dev/zero, which should not cause the memory to be committed immediately. – Pascal Cuoq Apr 15 '14 at 19:48
  • 1
    sure you can also align it yourself by returning an offset from a custom calloc using allocator, but you can't pass this or mmap memory to free, unlike aligned_alloc and posix_memalign allocated memory. But I am wondering if there is a reason for the omision besides minimizing the number of new functions for purities sake. – jtaylor Apr 15 '14 at 20:36
  • 2
    If you're going to roll your own with `mmap` like Pascal suggests, you'd just use `mmap(MAP_ANONYMOUS)` rather than actually opening `/dev/zero` and mmaping it. – Peter Cordes Apr 24 '17 at 02:34

1 Answers1

10

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.

Hans Passant
  • 873,011
  • 131
  • 1,552
  • 2,371
hanumantmk
  • 571
  • 3
  • 12
  • 2
    That's a good answer, but not very satisfactory. It basically begs the question of why specific platforms don't provide a non-standard API for this in the first place. AFAIK, not even Linux+glibc provide such a function. – Peter Cordes Apr 24 '17 at 02:32