加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

alignas说明符vs __attribute __(aligned),c 11

发布时间:2020-12-16 06:02:31 所属栏目:百科 来源:网络整理
导读:我目前正在C 11中开发一个操作系统内核,我遇到一个问题,似乎找不到自己的答案. 目前我正在调整我的分页结构,使用编译器特定的属性(例如,gcc的__attribute __(对齐)),但是我想使用C 11 alignas说明符,而不是Clang这是没有问题的,因为它很乐意接受4096对齐作为
我目前正在C 11中开发一个操作系统内核,我遇到一个问题,似乎找不到自己的答案.

目前我正在调整我的分页结构,使用编译器特定的属性(例如,gcc的__attribute __(对齐)),但是我想使用C 11 alignas说明符,而不是Clang这是没有问题的,因为它很乐意接受4096对齐作为对齐的参数,但是G不是!

所以首先,alignas说明符和gcc __attribute __(对齐)之间的主要区别是显然都能保证与特定值的对齐,但是gcc中的alignas说明符似乎有128的限制,而属性似乎几乎是无限,为什么是这样?

为什么不能将一个const整数传递给alignas说明符?

解决方法

从GCC支持状态来看,gcc 4.7中并没有完全支持对齐支持,但是它是 gcc 4.8. alignas也被列为4.8 release page中新支持的功能.

另外,从alignment support proposal(3.11):

A fundamental alignment is represented by an alignment less than or equal to the greatest alignment supported by the implementation in all contexts,which is equal to alignof(std::max_align_t) (18.1).

An extended alignment is represented by an alignment greater than
alignof(std::max_align_t). It is implementation-defined whether any extended
alignments are supported and the contexts in which they are supported (7.1.6). A type
having an extended alignment requirement is an over-aligned type.

同样的文件(7.1.6):

if the constant expression evaluates to an extended alignment and the implementation
does not support that alignment in the context of the declaration,the program is illformed

这可能也是答案的一部分.我目前无法访问完整的标准,有人应该可以确认.

对于__attribute __(aligned)和alignas之间的区别,我不认为它们在语义上是不同的,但是一个只是一个编译器扩展,另一个是由标准完全定义的.

为了回答你的最后一个问题,alignas只定义为:

alignas ( constant-expression ) 
alignas ( type-id )

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读