struct大小

对齐。

 #include <iostream>
using namespace std; struct S1 {
int a;
char b;
char c;
}; struct S2 {
int a;
char b;
char c;
double d;
}; struct S3 {
int a;
char b;
int c;
double d;
}; struct S4 {
int a;
char b;
int c;
double d;
char e[];
static char f[];
};
int main(int argc, char** argv) {
cout << "sizeof(S1): " << sizeof(S1) << endl;
cout << "sizeof(S2): " << sizeof(S2) << endl;
cout << "sizeof(S3): " << sizeof(S3) << endl;
cout << "sizeof(S4): " << sizeof(S4) << endl;
return ;
}

输出:

 root@xxj-VirtualBox:~/interview# ./struct
sizeof(S1):
sizeof(S2):
sizeof(S3):
sizeof(S4):

static变量是不占空间的。

上一篇:巧用Dictionary,完成客户需求


下一篇:WinSock 异步I/O模型