C语言结构体的字节对齐

Test Code:

 1 #include <iostream>
 2 #include <cstring>
 3 
 4 using namespace std;
 5 
 6 struct A{
 7     int a;
 8     short b;
 9     char d;
10     char e[10];
11 };
12 
13 struct B{
14     int a;
15     short b;
16     char *c;
17     char d;
18     char e[10];
19 };
20 
21 int main(){
22 
23     cout<<sizeof(A)<<endl;
24     cout<<sizeof(B)<<endl;
25     cout<<"short "<<sizeof(short)<<endl;
26     cout<<"int "<<sizeof(int)<<endl;
27     cout<<"long "<<sizeof(long)<<endl;
28     cout<<"char* "<<sizeof(char*)<<endl;
29 
30     return 0;
31 
32 }

Output(Fedora 64bit):

20
32
short 2
int 4
long 8
char* 8

 

posted @ 2016-04-18 10:35  hu983  阅读(144)  评论(0编辑  收藏  举报