alignas是否会影响sizeof的值? [英] Does alignas affect the value of sizeof?
问题描述
#include< iostream>
使用命名空间std;
int main()
{
alignas(double)unsigned char c [1024]; //字符数组,适合于双精度字符对齐
alignas(16)char d [100]; //在16个字节的边界上对齐
cout<< sizeof(c)<< endl;
cout<< sizeof(d)<< endl;
constexpr int n = alignof(int); //将int对齐到n个字节边界
cout<< n<< endl;
}
这是 alignas(double)的代码unsigned char c [1024];
,这意味着 c
应该与 double
对齐, double
是 8
个字节。
所以我认为 sizeof(c)
应该是 1024 * 8
个字节,但控制台输出是 1024
。
所以我很困惑。谁能告诉我原因?
alignas
关键字可以是用于指示对齐要求。例如, alignas(double)
强制变量具有与 double
相同的对齐要求。在我的平台上,这意味着变量将在8个字节的边界上对齐。
在您的示例中,整个数组将获得对齐要求,因此将其对齐在8个字节上字节边界,但这不会影响其大小。
但是 alignas
可能会更改a的大小。符合对齐要求时,复合数据类型需要附加填充。例如:
#include< iostream>
#include< cstddef>
结构测试
{
char a;
alignas(double)char b;
};
int main(int argc,char * argv [])
{
测试测试;
std :: cout<< 结构大小:<< sizeof(Test)<< std :: endl;
std :: cout<< ‘a’的大小:<< sizeof(test.a)<< std :: endl;
std :: cout<< ‘b’的大小:<< sizeof(test.b)<< std :: endl;
std :: cout<< ‘a’的偏移量:<< (int)offsetof(结构测试,a)<< std :: endl;
std :: cout<< ‘b’的偏移量:<< (int)offsetof(结构测试,b)<< std :: endl;
返回0;
}
输出:
结构大小:16
'a'的大小:1
'b'的大小:1
'a'的偏移量:0
'b'的偏移量:8
在我的平台上,此结构的大小甚至为16个字节尽管两个成员的大小均为1个字节。因此,由于对齐要求, b
并没有变大,但在 a
之后还有一些填充。您可以通过查看各个成员的大小和偏移量来看到它。 a
的大小仅为1个字节,但由于我们的对齐要求, b
的大小在偏移8个字节后开始。 / p>
并且结构的大小必须是其对齐方式的倍数,否则数组将不起作用。因此,如果您设置的对齐要求大于开始时整个结构的要求(例如,一个仅包含一个short的结构,并将alignas(double)应用于该数据成员),则必须在其后添加填充。
#include <iostream>
using namespace std;
int main()
{
alignas(double) unsigned char c[1024]; // array of characters, suitably aligned for doubles
alignas(16) char d[100]; // align on 16 byte boundary
cout<<sizeof(c)<<endl;
cout<<sizeof(d)<<endl;
constexpr int n = alignof(int); // ints are aligned on n byte boundarie
cout<<n<<endl;
}
Here is the code, for alignas(double) unsigned char c[1024];
, it means the c
should be aligned by double
, the double
is 8
bytes.
So I think sizeof(c)
should be 1024*8
bytes, but the console output is 1024
.
So I am confused. Who can tell me the reason?
The alignas
keyword can be used to dictate alignment requirements. alignas(double)
for example forces the variable to have the same alignment requirements as a double
. On my platform, this will mean that the variable is aligned on 8 byte boundaries.
In your example, the whole array will get the alignment requirements so it's being aligned on 8 byte boundaries but this won't affect its size.
It is however possible that alignas
changes the size of a composite data type when upholding the alignment requirements requires additional padding. Here's an example:
#include <iostream>
#include <cstddef>
struct Test
{
char a;
alignas(double) char b;
};
int main(int argc, char* argv[])
{
Test test;
std::cout << "Size of Struct: " << sizeof(Test) << std::endl;
std::cout << "Size of 'a': " << sizeof(test.a) << std::endl;
std::cout << "Size of 'b': " << sizeof(test.b) << std::endl;
std::cout << "Offset of 'a': " << (int)offsetof(struct Test, a) << std::endl;
std::cout << "Offset of 'b': " << (int)offsetof(struct Test, b) << std::endl;
return 0;
}
Output:
Size of Struct: 16
Size of 'a': 1
Size of 'b': 1
Offset of 'a': 0
Offset of 'b': 8
The size of this structure is 16 bytes on my platform even though both members are just 1 byte in size each. So b
didn't become bigger because of the alignment requirement but there is additional padding after a
. You can see this by looking at the size and offset of the individual members. a
is just 1 byte in size but b
, due to our alignment requirements, starts after a 8 byte offset.
And the size of a struct must be a multiple of its alignment, otherwise arrays don't work. So if you set an alignment requirement that's bigger than the whole struct was to begin with (for example a struct containing only a single short and you apply alignas(double) to that data member), padding must be added after it.
这篇关于alignas是否会影响sizeof的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!