Solved

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

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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
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 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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

856 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