?
Solved

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

Posted on 2015-01-24
4
Medium Priority
?
102 Views
Last Modified: 2015-02-11
23. What is the size of an <empty> object?
0
Comment
Question by:Nusrat Nuriyev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1000 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 1000 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 1000 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 1000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

762 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