C++ sizeof
1 #include <iostream>
2 using namespace std;
3
4 #define my_sizeof(type) (char *)(&type+1)-(char*)(&type)
5
6 union u
7 {
8 double a;
9 int b;
10 };
11
12 union u2
13 {
14 char a[13];
15 int b;
16 };
17
18 union u3
19 {
20 char a[13];
21 char b;
22 };
23
24 struct s4
25 {
26 int i: 8;
27 int j: 4;
28 int a:3;
29 double b;
30 };
31
32 int main()
33 {
34 double x;
35 string s("hello world");
36 cout << my_sizeof(x) << endl;
37 cout << sizeof(s) << endl;
38
39 char a[] = "abcdef";
40 cout << sizeof(a) << endl;
41
42 int b[20] = {3, 4};
43 cout << sizeof(b) << endl;
44
45 char c[2][3] = {"aa", "bb"};
46 cout << sizeof(c) << endl;
47
48 cout << sizeof(u) << endl; // 8
49 cout << sizeof(u2) << endl; // 16
50 cout << sizeof(u3) << endl; // 13
51
52 cout << sizeof(s4) << endl;
53 getchar();
54 return 0;
55 }
2 using namespace std;
3
4 #define my_sizeof(type) (char *)(&type+1)-(char*)(&type)
5
6 union u
7 {
8 double a;
9 int b;
10 };
11
12 union u2
13 {
14 char a[13];
15 int b;
16 };
17
18 union u3
19 {
20 char a[13];
21 char b;
22 };
23
24 struct s4
25 {
26 int i: 8;
27 int j: 4;
28 int a:3;
29 double b;
30 };
31
32 int main()
33 {
34 double x;
35 string s("hello world");
36 cout << my_sizeof(x) << endl;
37 cout << sizeof(s) << endl;
38
39 char a[] = "abcdef";
40 cout << sizeof(a) << endl;
41
42 int b[20] = {3, 4};
43 cout << sizeof(b) << endl;
44
45 char c[2][3] = {"aa", "bb"};
46 cout << sizeof(c) << endl;
47
48 cout << sizeof(u) << endl; // 8
49 cout << sizeof(u2) << endl; // 16
50 cout << sizeof(u3) << endl; // 13
51
52 cout << sizeof(s4) << endl;
53 getchar();
54 return 0;
55 }