首页 > 其他 > 详细

struct大小

时间:2014-10-01 17:59:21      阅读:232      评论:0      收藏:0      [点我收藏+]

对齐。

 1 #include <iostream>
 2 using namespace std;
 3 
 4 struct S1 {
 5     int a;
 6     char b;
 7     char c;
 8 };
 9 
10 struct S2 {
11     int a;
12     char b;
13     char c;
14     double d;
15 };
16 
17 struct S3 {
18     int a;
19     char b;
20     int c;
21     double d;
22 };
23 
24 struct S4 {
25     int a;
26     char b;
27     int c;
28     double d;
29     char e[9];
30     static char f[1000];
31 };
32 int main(int argc, char** argv) {
33     cout << "sizeof(S1): " << sizeof(S1) << endl;
34     cout << "sizeof(S2): " << sizeof(S2) << endl;
35     cout << "sizeof(S3): " << sizeof(S3) << endl;
36     cout << "sizeof(S4): " << sizeof(S4) << endl;
37     return 0;
38 }

 

输出:

1 root@xxj-VirtualBox:~/interview# ./struct 
2 sizeof(S1): 8
3 sizeof(S2): 16
4 sizeof(S3): 24
5 sizeof(S4): 40

 static变量是不占空间的。

struct大小

原文:http://www.cnblogs.com/linyx/p/4003373.html

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