Why is the size of an empty class in C++ not zero?

C++Sizeof

C++ Problem Overview


> Possible Duplicate:
> C++: What is the size of an object of an empty class?

Why does the following output 1?

#include <iostream>

class Test
{
};

int main()
{
	std::cout << sizeof(Test);
	return 0;
}

C++ Solutions


Solution 1 - C++

The standard does not allow objects (and classes thereof) of size 0, since that would make it possible for two distinct objects to have the same memory address. That's why even empty classes must have a size of (at least) 1.

Solution 2 - C++

> To ensure that the addresses of two > different objects will be different. > For the same reason, "new" always > returns pointers to distinct objects.

See Stroustrup for complete answer.

Solution 3 - C++

The C++ standard guarantees that the size of any class is at least one. The C++ standard states that no object shall have the same memory address as another object. There are several good reasons for this.

  1. To guarantee that new will always return a pointer to a distinct memory address.

  2. To avoid some divisions by zero. For instance, pointer arithmetics (many of which done automatically by the compiler) involve dividing by sizeof(T).

Note however that it doesn't mean that an empty base-class will add 1 to the size of a derived class:

struct Empty { };

struct Optimized : public Empty {
    char c;
};

// sizeof(Optimized) == 1 with g++ 4.0.1

Bjarne Stroustrup talks about this too.

Solution 4 - C++

Class without any data members and member function such type of class is known as empty class. Size of object of empty class is always 1 byte.

When we create object of any class at that time object always gets 3 characteristics i.e.

  1. State
  2. Behaviour
  3. Identity

When we create object of empty class at that time State of that object is nothing. Behaviour of that object is also nothing, but compiler assigns a unique address to that object. Memory in Computer is always organized in the form of bytes and minimum memory available at object address location is 1 byte. That's why size of object of empty class is 1 byte.

Solution 5 - C++

What Maurits and Péter said.

It is interesting to note in this context that compilers can do empty base class optimization (EBCO):

#include <iostream>
struct Foo {};
struct Bar : Foo {};
int main () {
    std::cout << sizeof(Foo) << ',' << sizeof(Bar) << std::endl;        
}

This will probably print "1,1" if you compile and run it. See also [Vandevoorde/Josuttis 16.2][1] on EBCO.

[1]: http://books.google.de/books?id=EotSAwuBkJoC&lpg=PA289&vq=EBCO&dq=faq&pg=PA289#v=snippet&q=EBCO&f=false%20%22empty%20base%20class%20optimization%22&pg=PA289#v=onepage&q=&f=false "C++ Templates: The Complete Guide, Section 16.2"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionshreyasvaView Question on Stackoverflow
Solution 1 - C++Péter TörökView Answer on Stackoverflow
Solution 2 - C++Maurits RijkView Answer on Stackoverflow
Solution 3 - C++wilhelmtellView Answer on Stackoverflow
Solution 4 - C++ShantanuView Answer on Stackoverflow
Solution 5 - C++Sebastian MachView Answer on Stackoverflow