aligned_alloc 函数要求 [英] aligned_alloc function requirements

查看:190
本文介绍了aligned_alloc 函数要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在看对aligned-alloc()的解释:http://en.cppreference.com/w/c/memory/aligned_alloc

I'm looking at the explanation of aligned-alloc(): http://en.cppreference.com/w/c/memory/aligned_alloc

void *aligned_alloc( size_t alignment, size_t size );

分配 size 字节的未初始化存储,其对齐方式由对齐方式指定.size 参数必须是对齐方式的整数倍."

"Allocate size bytes of uninitialized storage whose alignment is specified by alignment. The size parameter must be an integral multiple of alignment."

然而,示例代码是这样使用它的:

However, the example code uses it like this:

int *p2 = aligned_alloc(1024, 10*sizeof *p2);

10*sizeof *p 等于 40,所以它不是 1024 的整数倍.

10*sizeof *p equals 40, so it's not an integral multiple of 1024.

我有什么误解?

推荐答案

实际上,C11 标准似乎无法下定决心,并且不断改变要求:最初它是一个未定义的行为,然后随着 DR 460 他们改变了它失败并返回空指针,现在似乎他们想要改变它以接受任何值作为参数:http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2072.htm

Actually, it seems like C11 standard cannot make up its own mind, and keeps changing the requirements: initially it was an undefined behavior, then with DR 460 they changed it to fail and return null pointer, and now it seems like they want to change it to accept any values as arguments: http://www.open-std.org/jtc1/sc22/wg14/www/docs/n2072.htm

编译器似乎并不关心这些限制.

And compilers seem to not care about those restrictions.

这篇关于aligned_alloc 函数要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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