为什么C11中没有对齐的calloc [英] why is there no aligned calloc in C11

查看:122
本文介绍了为什么C11中没有对齐的calloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C11标准添加了aligned_alloc函数来分配未初始化的对齐内存. 该标准还包括calloc函数以分配已初始化为零的内存,但只能将其与最大类型的大小对齐.

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.

为什么C11标准不包括分配初始化为零的对齐内存的aligned_calloc函数?

Why does C11 standard not include an aligned_calloc function that allocate aligned memory that is zero initialized?

我知道您可以只设置结果以获取初始化的内存,但是calloc函数在某些操作系统上非常有用,因为为了安全起见,内核提供的内存通常必须已经初始化为零(并与页面大小对齐).原因. calloc可以利用它,避免重复初始化.

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.

另外,某些操作系统(如linux)以写时复制的方式提供内存,与calloc一起可以构造原始的稀疏数据结构.在aligned_alloc的结果上初始化memset会破坏此属性.

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.

推荐答案

我能提供的最佳猜测是,aligned_calloc特别违反了C1X宪章的明确目标之一:

The best guess I could offer is that an aligned_calloc specifically goes against one of the C1X charter's explicit goals:

与C9X不同,伦敦会议的共识是不应有C9X. 发明,无一例外.只有那些具有历史记录且具有共同点的功能 应考虑将其用于商业用途.还必须注意 标准化这些功能,使标准和商业 实现兼容.

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

环顾商业实现,alignment_malloc广泛可用,并且对于大多数平台都是通用的.对齐的calloc除了在许多平台上包装外,还需要更多的东西才能提供比alignment_malloc()+ memset()对更多的东西,因此可以被认为具有创造性,因此被排除在外.

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.

那是我最好的猜测.

这篇关于为什么C11中没有对齐的calloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆