Solved

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

Posted on 2015-01-24
4
96 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 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

Technology Partners: 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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

688 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