c语言 修改默认字节对齐

Modified on: Tue, 20 Aug 2019 10:58:00 +0800 热度: 2,010 度

编译器 gcc,vc++使用的编译器,clang 经过测试,默认字节对齐方式均为8字节

代码

#include <iostream>
using namespace std;


#pragma pack (8)
struct v1{
    int       a;
    long long b;
    char      c;
};
#pragma pack (4)
struct v2{
    int       a;
    long long b;
    char      c;
};
#pragma pack (2)
struct v3{
    int       a;
    long long b;
    char      c;
};
#pragma pack (1)
struct v4{
    int       a;
    long long b;
    char      c;
};
#pragma pack ()

int main(int argc, const char * argv[]) {
    cout<<sizeof(v1)<<endl;
    cout<<sizeof(v2)<<endl;
    cout<<sizeof(v3)<<endl;
    cout<<sizeof(v4)<<endl;
    return 0;
}

输出结果为:

24
16
14
13
Program ended with exit code: 0

添加新评论