内存对齐:如何使用alignof / alignas? [英] Memory alignment : how to use alignof / alignas?

查看:783
本文介绍了内存对齐:如何使用alignof / alignas?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用共享内存。



我不明白 alignof alignas



cppreference 不清楚: alignof 返回alignment,但什么是alignment?要为下一个要对齐的块添加的字节数?填充尺寸? 堆栈溢出 /博客条目也不清楚。 / p>

有人可以清楚地解释 alignof alignas p>

解决方案

对齐是一个限制,一个值的第一个字节可以存储在哪些内存位置。 (需要改进处理器上的性能并允许使用仅对具有特定对齐的数据有效的特定指令,例如SSE需要对齐到16字节,而AVX到32字节)



16的对齐表示16的倍数的内存地址是唯一有效的地址。

  alignas 

强制对齐到所需的字节数(cppreference没有提及它,但我认为你只能与2的权力对齐:1,2,4,8,16,32,64,128,...)

  #include < cstdlib> 
#include< iostream>


int main(){
alignas(16)int a [4];
alignas(1024)int b [4];
printf(%p \\\
,a);
printf(%p,b);

return 0;
}

示例输出:

  0xbfa493e0 
0xbfa49000 //注意现在有多少零。
//二进制等效
1011 1111 1010 0100 1001 0011 1110 0000
1011 1111 1010 0100 1001 0000 0000 0000 //每个零只是2的额外权力

其他关键字

  alignof 

非常方便,你不能做

  int a [4]; 
assert(a%16 == 0); //检查对齐是否为16个字节:WRONG编译器错误

但您可以

  assert(alignof(a)== 16); 
assert(alignof(b)== 1024);

注意,实际上这比一个简单的% Infact
我们知道对齐到1024字节的东西必须与1,2,4,8字节对齐,但

  assert(alignof(b)== 32); //失败。 

因此,为了更精确,alignof返回2的最大幂,



alignof是一个很好的方法来提前知道基本数据类型的最小对齐要求(它可能返回1为字符,4为float等)。



仍然合法:

  alignas(alignof(float))float SqDistance; 

对齐方式为16时,会放在下一个可用的地址16的倍数(可能存在来自上次使用的地址的隐式填充)


I work with shared memory right now.

I can't understand alignof and alignas.

cppreference is unclear : alignof returns "alignment" but what is "alignment" ? number of bytes to add for the next block to be aligned ? padded size ? Stack overflow / blogs entries are unclear too.

Can someone explain clearly alignof and alignas ?

解决方案

Alignment is a restriction on which memory positions a value's first byte can be stored. (it is needed to improve performance on processors and to permit use of certain instructions that works only on data with particular alignment, for example SSE need to be aligned to 16 bytes, while AVX to 32 bytes)

Alignment of 16 means that memory addresses that are a multiple of 16 are the only valid addresses.

alignas

force alignment to required number of bytes (cppreference does not mention it, but I think you can only align to powers of 2: 1,2,4,8,16,32,64,128,...)

#include <cstdlib>
#include <iostream>


int main(){
alignas(16) int a[4];
alignas(1024) int b[4];
printf("%p\n",a);
printf("%p",b);

return 0;
}

example output:

0xbfa493e0
0xbfa49000  //note how many more "zeros" now.
//binary equivalent
1011 1111 1010 0100 1001 0011 1110 0000
1011 1111 1010 0100 1001 0000 0000 0000 //every zero is just a extra power of 2

the other keyword

alignof

is very convenient, you cannot do something like

int a[4];
assert(a%16==0); //check if alignment is to 16 bytes: WRONG compiler error

but you can do

 assert( alignof(a)==16);
 assert( alignof(b)==1024);

note that in reality this is more strict than a simple "%" (modulus) operation. Infact we Know that something aligned to 1024 bytes is necessarily aligned to 1,2,4,8 bytes but

 assert( alignof(b)==32); //fail.

So to be more precise, "alignof" returns the greatest power of 2 to wich something is aligned.

Also alignof is a nice way to know in advance minimum alignment requirement for basic datatypes (it will probably return 1 for chars, 4 for float etc.)

Still legal:

alignas(alignof(float)) float SqDistance;

Something with an alignment of 16 then will be placed on the next available address that is a multiple of 16 (there may be a implicit padding from last used address)

这篇关于内存对齐:如何使用alignof / alignas?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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