96

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;
}
Community
  • 1
  • 1
shreyasva
  • 13,126
  • 25
  • 78
  • 101
  • 7
    There is a dummy placeholder member whose size happens to be one byte. Since for an array of Test[10], each object should have a unique address. – legends2k Mar 02 '10 at 10:06
  • 6
    An interesting optimization though is the `Empty Base Optimization`, meaning that if you inherit from an empty base class (no attribute, no virtual methods), then your class size won't grow. There are a number of (other) conditions, but it explains why privately inheriting from predicates in some situations. – Matthieu M. Mar 02 '10 at 15:55
  • 1
    [similar question 1](http://stackoverflow.com/questions/1626446) and [similar question 2](http://stackoverflow.com/questions/621616/). – Lazer May 16 '10 at 06:11

5 Answers5

135

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.

Péter Török
  • 114,404
  • 31
  • 268
  • 329
  • 3
    Hm... but shouldn't the linker be able to take care of that regardless of what sizeof() returns? Isn't this more like a side-effect? I understand what you are saying, but isn't it perfectly doable to return 0 for sizeof(Test). But if the standard says so, it says so. Actually a good thing for once to be explicit instead of intentionally vague about a subject. – Prof. Falken Mar 02 '10 at 10:07
  • 17
    @Amigable, so what would `Test a[10];` have as size? And `sizeof a / sizeof *a` would divide by 0. And `for(Test *i = a; i != a + 10; i++) f(i);` would also fail to work. I believe it would cause a lot of problems, as you need a lot of special cases in compilers *and* in user code. – Johannes Schaub - litb Mar 02 '10 at 12:21
  • @Johannes, so true. I did not think about that. – Prof. Falken Mar 03 '10 at 13:55
  • 2
    @JohannesSchaub-litb: I disagree with your comment. sizeof a / sizeof *a would be 0/0 which would be consistent and "proper" behavior for zero size types (this is because Test a[10] and Test a[100] can not be distinguished, which is logical since they are both degenerate}). Also the for loop would NOT fail. This is because a + 10 = a. I dont see any logical inconsistency with zero size types, IMO not allowing them introduces inconsistencies. The only advantage this has is probably just the requirement that all objects have distinct addresses. – Andreas H. Oct 18 '13 at 12:46
  • 1
    @AndreasH.: I mostly agree with you, and I think `sizeof(Empty)` should have been zero. But it is a bit more complicated because `for(Test *i = a; i != a + 10; ++i) f(*i);` would do a different thing from `for(int i = 0; i < 10; ++i) f(a[i])`. I've analyzed it in detail, including a proposed solution, at [Legalization of empty objects](http://stannum.co.il/blog/4/legalization-of-empty-objects). – Yakov Galka Jun 25 '16 at 09:33
38

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.

Maurits Rijk
  • 9,789
  • 2
  • 36
  • 53
31

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.

mic
  • 821
  • 6
  • 18
wilhelmtell
  • 57,473
  • 20
  • 96
  • 131
  • 2
    What pointer arithmetic involves _dividing_ by `sizeof(T)`? I can't think of a single example. Plase add at least one example. – MSalters Mar 02 '10 at 13:55
  • 6
    @MSalters: iterating over arrays of elements of type T. – wilhelmtell Mar 02 '10 at 16:32
  • 1
    @MSalters: subtraction of two pointers returns the number of elements, not the number of bytes, in between. – Ben Voigt Oct 11 '10 at 01:53
  • 1
    @Ben Voigt: good point, you're right. Iterating over an array just means _adding_ `sizeof(T)` bytes to a pointer, but a poitner difference internally will be calculated first as a byte difference. – MSalters Oct 11 '10 at 09:05
13

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.

sjngm
  • 12,423
  • 14
  • 84
  • 114
Shantanu
  • 131
  • 1
  • 2
  • if any one wanted to knw abt 3 charecteristics of an object i.e. 1)State 2)Behaviour 3)Identity then ask me – Shantanu Oct 17 '11 at 08:13
4

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 on EBCO.

Sebastian Mach
  • 38,570
  • 8
  • 95
  • 130