c++ - alignas会影响sizeof吗
天蓬老师
天蓬老师 2017-04-17 15:39:12
0
1
949
#include <iostream>

using namespace std;
struct alignas(32) S{
    char a;
};

char alignas(4) arr[3];

int main()
{
    cout << sizeof(S) << endl; //output 32
    cout << sizeof(arr) << endl;//output 3
    
    return 0;
}

为什么数组的输出是3呢?为什么这里的alignas没有影响sizeof,而对struct 又影响了大小呢?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(1)
迷茫

When

alignas is used in type declaration, it will affect the padding inside the type, and this part of padding is also counted as the size of the type. Will affect sizeof.

When

alignas is used in variable declaration , it only requires the runtime to align the address of this variable accordingly. Does not affect sizeof.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!