博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Why is the size of an empty class not zero in C++?
阅读量:4322 次
发布时间:2019-06-06

本文共 2992 字,大约阅读时间需要 9 分钟。

 

 

  Predict the output of the following program?

1 #include
2 using namespace std; 3 4 class Empty 5 { 6 }; 7 8 int main() 9 {10 cout << sizeof(Empty) << endl;11 return 0;12 }

   Output:  1

  

  Size of an empty class is not zero. It is 1 byte generally. It is non-zero to ensure that the two different objects will have different addresses.

  See the following example.

1 #include
2 using namespace std; 3 4 class Empty 5 { 6 }; 7 8 int main() 9 {10 Empty a, b;11 12 if (&a == &b)13 {14 cout << "impossible " << endl;15 }16 else17 {18 cout << "Fine " << endl;19 }20 21 return 0;22 }

  Output:   Fine

 

  For the same reason (different objects should have different addresses), “new” always returns pointers to distinct objects.

  See the following example.

 

 

1 #include
2 using namespace std; 3 4 class Empty 5 { 6 }; 7 8 int main() 9 {10 Empty* p1 = new Empty;11 Empty* p2 = new Empty;12 13 if (p1 == p2)14 {15 cout << "impossible " << endl;16 }17 else18 {19 cout << "Fine " << endl;20 }21 return 0;22 }

  Output:  Fine

 

  Now guess the output of following program (This is tricky).

1 #include
2 using namespace std; 3 4 class Empty 5 { 6 }; 7 8 class Derived: Empty 9 { 10 int a; 11 };12 13 int main()14 {15 cout << sizeof(Derived);16 return 0;17 }

  Output (with GCC compiler):    4

  

  Note that the output is not greater than 4. There is an interesting rule that says that an empty base class need not be represented by a separate byte. So compilers are free to make optimization in case of empty base classes.

  As an excercise, try the following program on your compiler.

1 #include 
2 using namespace std; 3 4 class Empty 5 { 6 }; 7 8 class Derived1 : public Empty 9 {10 };11 12 class Derived2 : virtual public Empty13 {14 };15 16 class Derived3 : public Empty17 { 18 char c;19 };20 21 class Derived4 : virtual public Empty22 {23 char c;24 };25 26 class Dummy27 {28 char c;29 };30 31 int main()32 {33 cout << "sizeof(Empty) " << sizeof(Empty) << endl;34 cout << "sizeof(Derived1) " << sizeof(Derived1) << endl;35 cout << "sizeof(Derived2) " << sizeof(Derived2) << endl;36 cout << "sizeof(Derived3) " << sizeof(Derived3) << endl;37 cout << "sizeof(Derived4) " << sizeof(Derived4) << endl; 38 cout << "sizeof(Dummy) " << sizeof(Dummy) << endl;39 40 return 0;41 }

  运行结果如下所示。

  

 

  补充:

  运行如下代码,运行结果为:  1

1 #include    
2 #include
3 using namespace std; 4 class Test 5 { 6 int arr[0]; 7 }; 8 9 int main()10 {11 Test t;12 cout<

 

 

 

 

  Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

 

  转载请注明:

  2013-11-25  20:39:10

 

  

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/iloveyouforever/p/3442162.html

你可能感兴趣的文章
Ural 1001 - Reverse Root
查看>>
玩转webpack之webpack的entry output
查看>>
java 操作mongodb查询条件的常用设置
查看>>
黑马程序员_java基础笔记(02)...java语言基础组成
查看>>
关于缓存击穿
查看>>
对innodb 拷贝文件实现数据库的方式(转)
查看>>
python知识点 2014-07-09
查看>>
FloatingActionButton的一点学习感悟
查看>>
ABAP CDS ON HANA-(10)項目結合して一つ項目として表示
查看>>
网站地址信息
查看>>
产品经理 - 登录 注册
查看>>
小白的python进阶历程------05.占位符
查看>>
CF414BMashmokh and ACMDP
查看>>
Notepad++ 通过g++编译
查看>>
JAVA基础2——类初始化相关执行顺序
查看>>
转:Zend Framework 重定向方法(render, forward, redirect)
查看>>
Linux下查看磁盘与目录的容量——df、du
查看>>
关于日记app的思考
查看>>
使用sencha的cmd创建项目时提示找不到\Sencha\Cmd\repo\.sencha\codegen.json
查看>>
如何快速启动一个Java Web编程框架
查看>>