Solved

23. What is the size of an <empty> object?

Posted on 2015-01-24
4
70 Views
Last Modified: 2015-02-11
23. What is the size of an <empty> object?
0
Comment
Question by:Nusrat Nuriyev
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 40568253
Give it a try ;o)

#include <iostream>

class Empty {

};

int main() {

  std::cout << "Size of Empty is " << sizeof(Empty) << std::endl;

  return 0;
}

Open in new window


VC++ says:
Size of Empty is 1
g++ says:
Size of Empty is 1
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 250 total points
ID: 40568641
>> Size of Empty is 1
But why 1? Why not 0? Could it be 0 on some other platform?
From: http://www.stroustrup.com/bs_faq2.html#sizeof-empty
Why is the size of an empty class not zero?
To ensure that the addresses of two different objects will be different. For the same reason, "new" always returns pointers to distinct objects.
. . .
There is an interesting rule that says that an empty base class need not be represented by a separate byte:
	struct X : Empty {
		int a;
		// ...
	};

	void f(X* p)
	{
		void* p1 = p;
		void* p2 = &p->a;
		if (p1 == p2) cout << "nice: good optimizer";
	}

Open in new window

This optimization is safe and can be most useful. It allows a programmer to use empty classes to represent very simple concepts without overhead. Some current compilers provide this "empty base class optimization".
BTW, from the link, you will find many other questions answered by one of the world's most authoritative authors on C++.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 250 total points
ID: 40568656
>>But why 1? Why not 0? Could it be 0 on some other platform?

That's why I formulated exactly like that ;o)

My initial idea would have been "implementation dependant". Yet the ones I had available were not enough to make a point, so thank you for shedding in some light.
0
 
LVL 32

Assisted Solution

by:phoffric
phoffric earned 250 total points
ID: 40568717
Those were my initial questions when I saw your result. So I looked to Stroustrup for an answer. Luckily I found it; otherwise, I would have to go through the wonderful C++ iso specification.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now