Why is the size of an empty class not zero in C++?
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++?的更多相关文章
- [转载] C++ STL中判断list为空,size()==0和empty()有什么区别
关于两个的区别,首先size()==0为bool表达式,empty()为函数调用,这一点很明显.查看源代码, bool empty() const { return _M_node->_M_ne ...
- 数据结构:队列queue 函数push() pop size empty front back
队列queue: push() pop() size() empty() front() back() push() 队列中由于是先进先出,push即在队尾插入一个元素,如:可以输出:Hello W ...
- 空基类优化empty base class optimization
1.为什么C++中不允许类的大小是0 class ZeroSizeT {}; ZeroSizeT z[10]; &z[i] - &z[j]; 一般是用两个地址之间的字节数除以类型大小而 ...
- [LeetCode] Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- [LeetCode] Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- [LeetCode] House Robber II 打家劫舍之二
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- [LeetCode] House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- leetcode笔记
82. Remove Duplicates from Sorted List II https://leetcode.com/problems/remove-duplicates-from-sorte ...
- Maximal Rectangle
很不好想的一道题,参考:http://blog.csdn.net/doc_sgl/article/details/11832965 分为两步:把原矩阵转为直方图,再用largest rectangle ...
随机推荐
- 跟着老猫来搞GO,基础进阶
回顾一下上一篇博客,主要是和大家分享了GO语言的基础语法,其中包含变量定义,基本类型,条件语句,循环语句.那本篇呢就开始和大家同步一下GO语言基础的进阶. 函数的定义 上次其实在很多的DEMO中已经写 ...
- D3.js V5 教程
D3.js V5 教程 1.在项目中使用D3.js 2. 选择元素和设置(获取)属性 3. 绑定数据 4. 理解Update.Enter.Exit 与 添加.删除元素 未完待续..........
- Mplus数据分析:随机截距交叉之后的做法和如何加协变量,写给粉丝
记得之前有写过如何用R做随机截距交叉滞后,有些粉丝完全是R小白,还是希望我用mplus做,今天就给大家写写如何用mplus做随机截距交叉滞后. 做之前我们需要知道一些Mplus的默认的设定: obse ...
- [bzoj5343]混合果汁
二分枚举答案,问题转化为计算至少取到一定体积,价格最少是多少,显然是贪心取最小,用线段树维护,然后因为要判断答案,所以可持久化一下即可. 1 #include<bits/stdc++.h> ...
- arm中断汇编
IRQ_Handler: push {lr} /* 保存 lr 地址 */ push {r0-r3, r12} /* 保存 r0-r3,r12 寄存器 */ mrs r0, spsr /* 读取 sp ...
- java的String参数格式化
String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语言的sprintf()方法,两者有类似之处.format()方法有两种重载形式. form ...
- Golang - 关于 proto 文件的一点小思考
目录 前言 helloworld.proto 小思考 小结 推荐阅读 前言 ProtoBuf 是什么? ProtoBuf 是一套接口描述语言(IDL),通俗的讲是一种数据表达方式,也可以称为数据交换格 ...
- 如何理解Casbin的权限控制
概念: Casbin是什么? Casbin是一个访问控制框架,可以支持多种访问控制模型(如ACL.RBAC.ABAC等) 目的: 我们最终想要实现的效果: 可以控制某一个人/角色(sub)能否对某个资 ...
- 【基因组预测】braker2基因结构注释要点记录
目录 流程使用 问题 记录下braker2的使用要点,以备忘记. 流程使用 braker2有很多流程,根据你的数据:组装的基因组.转录组.蛋白(同源,包括近缘或远缘)选择不同流程,官网有说明: htt ...
- 毕业设计之zabbix---自带模板监控mysql内容
自带模板是不能直接建立连接就可以用的 必须经历一下几步: 建立用户权限: [root@mysql.quan.bbs lib]$mysql -u root -p Enter password: Welc ...