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. APP 自动化之appium元素定位(三)

    APP自动化测试关键环节--元素定位,以下我们来了解appium提供的元素定位方法! 1. id定位,id一个控件的唯一标识,由开发人员在项目中指定,如果一个元素有对应的resource-id,我们就 ...

  2. selenium2.x 与 selenium3.x 最大区别

    一.selenium2.x 与 selenium3.x 最大区别 (1) 从3.0版本selenium开始使用火狐浏览器完成web自动化就需要用到驱动包了. (2) 而2.0版本的selenium使用 ...

  3. Java8新特性之方法引用&Stream流

    Java8新特性 方法引用 前言 什么是函数式接口 只包含一个抽象方法的接口,称为函数式接口. 可以通过 Lambda 表达式来创建该接口的对象.(若 Lambda 表达式抛出一个受检异常(即:非运行 ...

  4. 从环境搭建到回归神经网络案例,带你掌握Keras

    摘要:Keras作为神经网络的高级包,能够快速搭建神经网络,它的兼容性非常广,兼容了TensorFlow和Theano. 本文分享自华为云社区<[Python人工智能] 十六.Keras环境搭建 ...

  5. RabbitMQ 线上事故!慌的一批,脑袋一片空白。。。

    前言 那天我和同事一起吃完晚饭回公司加班,然后就群里就有人@我说xxx商户说收不到推送,一开始觉得没啥.我第一反应是不是极光没注册上,就让客服通知商户,重新登录下试试.这边打开极光推送的后台进行检查. ...

  6. [hdu6974]Destinations

    注意到一个人的三条链一定不会同时选(忽略仅选一个终点的限制),因为其有公共点(起点) 换言之,问题相当于给定$3m$条链,选择$m$条没有公共点的链,并最小化代价和 进一步的,显然也不存在多于$m$条 ...

  7. List、ArrayList、迭代器、链表、Vector

    1.List接口中的常用方法. List是Collection接口的子接口.所以List接口中有一些特有的方法. void add(int index, Object element) Object ...

  8. 链式调用Builder

    使用Lombok实现链式调用 1.静态调用 User对象: 对象中必须有一个值不为空staticname作为指定的参数并调用对象 @Accessors(chain = true) @Getter @S ...

  9. 爬虫——正则表达式爬取豆瓣电影TOP前250的中英文名

    正则表达式爬取豆瓣电影TOP前250的中英文名 1.首先要实现网页的数据的爬取.新建test.py文件 test.py 1 import requests 2 3 def get_Html_text( ...

  10. OI省选算法汇总及学习计划(转)

    1.1 基本数据结构 数组(√) 链表(√),双向链表(√) 队列(√),单调队列(√),双端队列(√) 栈(√),单调栈(√) 1.2 中级数据结构 堆(√) 并查集与带权并查集(√) hash 表 ...