C语言结构体的字节对齐

时间:2021-05-03 14:58:42

Test Code:

 #include <iostream>
#include <cstring> using namespace std; struct A{
int a;
short b;
char d;
char e[];
}; struct B{
int a;
short b;
char *c;
char d;
char e[];
}; int main(){ cout<<sizeof(A)<<endl;
cout<<sizeof(B)<<endl;
cout<<"short "<<sizeof(short)<<endl;
cout<<"int "<<sizeof(int)<<endl;
cout<<"long "<<sizeof(long)<<endl;
cout<<"char* "<<sizeof(char*)<<endl; return ; }

Output(Fedora 64bit):

20
32
short 2
int 4
long 8
char* 8