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 ...
随机推荐
- 什么是 Webhook?
1. 什么是 Webhook? Webhook 是一个 API 概念,是微服务 API 的使用范式之一,也被成为反向 API,即前端不主动发送请求,完全由后端推送:举个常用例子,比如你的好友发了一条朋 ...
- jenkins 生成HTML报表,邮件推送
1.登录jenkins,系统管理=>插件管理 =>可选插件安装 安装成功: 2.打开任务,进入配置 3.添加构建后操作 4.配置页面 5.构建后report输出配置完成后点击立即构建,构建 ...
- Linux配置2个Tomcat同时运行
先说一下怎么遇到的这个问题,在练习linux中部署web项目时,linux系统安装了两个tomcat. 操作步骤: 1.配置profile#vi /etc/profile 输入以下内容: 这是两个to ...
- Linux mem 2.6 Rmap 内存反向映射机制
文章目录 1. 简介 2. 匿名内存 Rmap 的建立 2.1 fork() 2.2 do_page_fault() 3. 文件内存 Rmap 的建立 3.1 fork() 3.2 do_page_f ...
- Mac下Shell脚本使用学习笔记(二)
参考文献 Shell 教程 MAC常用终端命令行 Mac下Shell脚本使用 (7)Shell echo命令: 命令格式:echo string ①显示普通字符串:echo "It is a ...
- mysql 数据库中 int(3) 和 int(11) 有区别么???
今天去面试的时候 面试官问到了这个问题:int(3) 和 int(11) 有什么区别?? 当时一听有点蒙,(不知道为什么蒙,后来回来想想可能是觉得考官怎么会问这么简单的问题呢,所以蒙了),当时我的回答 ...
- 使用pmml跨平台部署机器学习模型Demo——房价预测
基于房价数据,在python中训练得到一个线性回归的模型,在JavaWeb中加载模型完成房价预测的功能. 一. 训练.保存模型 工具:PyCharm-2017.Python-39.sklearn2 ...
- [loj3340]命运
容斥,强制若干条链不重要,即有$2^{n-1-s}$种(其中$s$为这些链的并所覆盖的边数),暴力将选中的链打标记,时间复杂度$o(m^{2}2^{m}+n\log_{2}n)$(预处理出这$2m$个 ...
- 【POJ3614 Sunscreen】【贪心】
题面: 有c头牛,需要的亮度在[min_ci,max_ci]中,有n种药,每种m瓶,可以使亮度变为v 问最多能满足多少头牛 算法 我们自然考虑贪心,我们首先对每头牛的min进行排序,然后对于每种药,将 ...
- Codeforces 1606F - Tree Queries(虚树+树形 dp)
Codeforces 题面传送门 & 洛谷题面传送门 显然我们选择删除的点连同 \(u\) 会形成一个连通块,否则我们如果选择不删除不与 \(u\) 在同一连通块中的点,答案一定更优. 注意到 ...