代码改变世界

Class,strict sizeof用法

2022-11-13 15:26  钟铧若岩  阅读(17)  评论(1编辑  收藏  举报

 

 1 #include "iostream"
 2 using namespace std;
 3 
 4 class A{};
 5 class A2{char d,e;};
 6 struct B{};
 7 struct C{char b,c;};
 8 struct D{int x,y;};
 9 
10 int main()
11 {
12 
13     cout << "sizeof(A) = " << sizeof(A) <<endl; //1 空类的长度就是1
14     cout << "sizeof(A2) = " << sizeof(A2) <<endl;   //2 
15 
16     A *p1 = new A();
17     A p2;
18     A *p3;
19 
20     cout << "sizeof(p1) = " << sizeof(p1) << endl; //8
21     cout << "sizeof(p2) = " << sizeof(p2) << endl; //1
22     cout << "sizeof(p3) = " << sizeof(p3) << endl; //8
23 
24     cout << "sizeof(B) = " << sizeof(B) <<endl;//1
25     cout << "sizeof(C) = " << sizeof(C) <<endl;//2
26     cout << "sizeof(D) = " << sizeof(D) <<endl;//8
27 
28 
29 
30     cout << "hello world" << endl;
31     return 0;
32 }