Predict the output of the following program?

 1 #include<iostream>
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<iostream>
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 else
17 {
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<iostream>
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 else
18 {
19 cout << "Fine " << endl;
20 }
21 return 0;
22 }

  Output:  Fine

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

 1 #include<iostream>
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 <iostream>
2 using namespace std;
3
4 class Empty
5 {
6 };
7
8 class Derived1 : public Empty
9 {
10 };
11
12 class Derived2 : virtual public Empty
13 {
14 };
15
16 class Derived3 : public Empty
17 {
18 char c;
19 };
20
21 class Derived4 : virtual public Empty
22 {
23 char c;
24 };
25
26 class Dummy
27 {
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    <cstdio>
2 #include <iostream>
3 using namespace std;
4 class Test
5 {
6 int arr[0];
7 };
8
9 int main()
10 {
11 Test t;
12 cout<<sizeof(t);
13 return 0;
14 }

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

  转载请注明:http://www.cnblogs.com/iloveyouforever/

  2013-11-25  20:39:10

  

  

Why is the size of an empty class not zero in C++?的更多相关文章

  1. [转载] C++ STL中判断list为空,size()==0和empty()有什么区别

    关于两个的区别,首先size()==0为bool表达式,empty()为函数调用,这一点很明显.查看源代码, bool empty() const { return _M_node->_M_ne ...

  2. 数据结构:队列queue 函数push() pop size empty front back

    队列queue: push() pop() size() empty() front() back() push()  队列中由于是先进先出,push即在队尾插入一个元素,如:可以输出:Hello W ...

  3. 空基类优化empty base class optimization

    1.为什么C++中不允许类的大小是0 class ZeroSizeT {}; ZeroSizeT z[10]; &z[i] - &z[j]; 一般是用两个地址之间的字节数除以类型大小而 ...

  4. [LeetCode] Implement Queue using Stacks 用栈来实现队列

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  5. [LeetCode] Implement Stack using Queues 用队列来实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  6. [LeetCode] House Robber II 打家劫舍之二

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  7. [LeetCode] House Robber 打家劫舍

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  8. leetcode笔记

    82. Remove Duplicates from Sorted List II https://leetcode.com/problems/remove-duplicates-from-sorte ...

  9. Maximal Rectangle

    很不好想的一道题,参考:http://blog.csdn.net/doc_sgl/article/details/11832965 分为两步:把原矩阵转为直方图,再用largest rectangle ...

随机推荐

  1. OpenHarmony LiteOS C-SKY指令集移植指北

    摘要:本文介绍在OpenHarmony社区LiteOS-M项目中新增C-SKY指令集的开发流程,以及适配相应qemu工程的方法和步骤,供LiteOS内核相关开发者学习交流. 本文分享自华为云社区< ...

  2. Java学习(九)

    今天先学习了内联框架的知识,使用iframe的标签,还有超链接的知识. 做了个小实践 <!DOCTYPE html> <head> <meta charset=" ...

  3. 如何修改visual-studio的sln文件和project工程名

    关于VS的 .sln 文件和 .suo 文件 *.sln:(Visual Studio.Solution) 通过为环境提供对项目.项目项和解决方案项在磁盘上位置的引用,可将它们组织到解决方案中.比如是 ...

  4. python-文件操作(一)

    目录 文件操作 1.什么是文件? 2.操作文件的方法: 3.路径分类: 4.如何取消特殊字符的功能: 5.对文件的操作有:读.写.追加内容 6.with上下文管理 7.文件操作方法详细: 1.r-读操 ...

  5. Java-ASM框架学习-修改类的字节码

    Tips: ASM使用访问者模式,学会访问者模式再看ASM更加清晰 ClassReader 用于读取字节码,父类是Object 主要作用: 分析字节码里各部分内容,如版本.字段等等 配合其他Visit ...

  6. es基本概念

    电商实时数据分析平台需要学些什么?  [运营指标,流量指标,销售转化指标,客户价值指标,商品指标,营销指标,风险控制指标,市场竞争指标]等电商指标数据. 检索,数据更新,排序,分词,query等缓存机 ...

  7. 8.3 k8s部署jenkins,通过pv/pvc结合NFS服务器持久化

    1.制作jenkins docker镜像 1.1 下载jenkins wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/war-stable/2.30 ...

  8. 2017年最有前景的十大IT职业岗位

    在IT行业,并不常存在失业的现象,因为目前整个行业存在严重的专业人才供给不足的现象:但同样,想要进入这个行业并牢牢站稳脚跟,你也需要拥有更强于其他行业的竞争力和承受更大的压力.那在行业中,哪些职位更有 ...

  9. LOJ 3399 -「2020-2021 集训队作业」Communication Network(推式子+组合意义+树形 DP)

    题面传送门 一道推式子题. 首先列出柿子,\(ans=\sum\limits_{T_2}|T_1\cap T_2|·2^{T_1\cap T_2}\) 这个东西没法直接处理,不过注意到有一个柿子 \( ...

  10. R语言与医学统计图形-【9】过渡函数qplot

    ggplot2绘图系统 基础绘图包向ggplot2过渡--qplot 绘图理念的不同: 基础绘图包是先铺好画布,再在这张画布上作图(常规思维): ggplot2打破常规,采用图层叠加的方法. qplo ...