“最小对齐"与“最小对齐"之间的区别是什么?和“首选对齐方式"? [英] What is the difference between "minimum alignment" and "preferred alignment"?
问题描述
最近,我发现在Clang 9.0上, alignof 和 __ alignof 返回的值分别为 unsigned long long ,并且在 https://reviews.llvm.org/D54814 :
Recently I observed that on Clang 9.0 alignof and __alignof are returning different values for unsigned long long and the same has been discussed at https://reviews.llvm.org/D54814:
从Clang 8.0和GCC 8.0开始,
alignof
和__ alignof
在相同情况下返回不同的值.具体来说,alignof
和_Alignof
返回类型的最小对齐方式,其中__ alignof
返回首选对齐方式.
Starting in Clang 8.0 and GCC 8.0,
alignof
and__alignof
return different values in same cases. Specificallyalignof
and_Alignof
return the minimum alignment for a type, where as__alignof
returns the preferred alignment.
我了解类型对齐方式,但从未遇到过最小对齐方式"和首选对齐方式".
I know about type alignment but never came across "minimum alignment" and "preferred alignment".
有人可以帮我了解一下到底是什么,有什么区别吗?谢谢.
Could somebody please help me understand what exactly these are and what is the difference? Thanks.
推荐答案
(在给定平台上)最小对齐是不会崩溃的对齐方式.在x86-64上,它是一个字节.在
The minimal alignment is (on a given platform) the one which won't give crashes. On x86-64 it is one byte. On PowerPC or Sparc or RISC-V it is probably 4 or 8 bytes.
首选对齐方式是通常的对齐方式,例如由于处理器总线或CPU缓存.在x86-64上, unsigned long long
可能是8个字节.对齐方式较差的访问都会降低性能.
The preferred alignment is the one which is usual, e.g. because of processor bus or CPU caches. On x86-64 for unsigned long long
it probably is 8 bytes. Any less aligned access has a performance penalty.
详细信息是目标处理器,并且特定于 ABI (例如,请参见交叉编译器.
Details are target processor and ABI specific (for example, see this). Think of cross-compilers.
C或C ++的语义的定义不完善,而且不完全正式化.查看C ++草案标准:它是用英语写的,没有形式化.但另请参见 Frama-C (它具有C ++的实验性前端)和未定义的行为.
The semantics of C or of C++ is not perfectly defined and not fully formalized. Look into the C++ draft standard: it is written in English, not formalized. But see also Frama-C (it has an experimental front end for C++) and CompCert. Read about undefined behavior.
这篇关于“最小对齐"与“最小对齐"之间的区别是什么?和“首选对齐方式"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!