首页 > 编程语言 > 详细

C语言结构体的字节对齐

时间:2016-04-18 11:26:29      阅读:148      评论:0      收藏:0      [点我收藏+]

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

 

C语言结构体的字节对齐

原文:http://www.cnblogs.com/hu983/p/5403508.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!