tags:

views:

180

answers:

5

Basically it is a follow up of this question..

When I look into the Standard docs I found this..

In Classes 9.3,

Complete objects and member subobjects of class type shall have nonzero size.96) ...

Yeah, true.. But,

96)Base class subobjects are not so constrained.

So, when I looked into Stroustrup's FAQ, there is an example as

void f(X* p)
    {
        void* p1 = p;
        void* p2 = &p->a;
        if (p1 == p2) cout << "nice: good optimizer";
    } 

My question is I couldn't understand how it is an optimization and also why base classes are allowed to have zero size?

+10  A: 

If the base class is empty, you will never need to have the base class object's or any of its members' address (independent of the derived class objects's address, that is), so it is legal to optimize its size away.

That saves you (at least) one byte of memory (can be more due to memory alignment rules), which can add up to significant savings if you have millions of such objects in your app on a memory-constrained platform.

Péter Török
Yeah.. Thanks.. :)
liaK
+8  A: 

Base classes cannot have zero size. Only base class subobjects can. Meaning the base part of the derived object.

Armen Tsirunyan
+1, Simple and clear... :)
liaK
A: 

I'm no Raymond Chen, but I can play the 'what if it were true' game.

If a class that can be passed as reference can be zero-size, then at some point it might be passed to malloc(0), which may return NULL or a dereferencable address. Then two instances could appear equal when they should not.

If however it is a member of another class or a base-class, its address and size are derived from its placement in the containing class allocation, and it is safe for it to be zero size.

And being zero size is good for memory efficiency.

Will
"If however it is a member of another class, its address and size are derived from its placement in the containing class allocation, and it is safe for it to be zero size." A member subobect cannot have zero size either. Only base-class subobject
Armen Tsirunyan
@Amen Tsirunyan, interesting; why? To preserve FOFF macros?
Will
@Will Not sure WHY, but the standars is clear in this respect. But that's a good point, I'll give it some thinking, maybe it has something to do with pointers-to-members...
Armen Tsirunyan
Raymond Chen's 'what if it were true' game is a fun way to work this out
Will
@Will, @Armen, if an object of a class can be a member of another class, it can also be a standalone variable. _(But it is illegal to have two distinct variables share the same memory address.)_ Or an element of an array. _(But then how you are going to iterate through or do pointer arithmetic over such an array?)_
Péter Török
A: 

It's an optimization because instances of the derived class take up less space than they would have, if the base class subobjects were required to have non-zero size. It means that base classes such as interfaces, or mix-ins, or templated policy classes, don't increase the size of classes that implement or use them. Ultimately, your program needs less memory to do the same thing.

I'm not terribly sure on the history, but the impression I get is that sometime in the 1990s, some compilers started doing it, and the standards committee decided to standardize the existing practice. I think the proliferation of allocator objects in STL templates was part of the reason - a std::vector typically would be the size of 3 pointers with the empty base optimization, and 4 pointers without it (due to alignment). Here's an article from 1997 discussing it - it's clear that it wasn't all that widespread when that was written, but it's basically standard practice now.

Doug
A: 

Size of an object is nothing but the cumulative size of it's members. And NO, no class can have a zero size, even an empty class will have a size of 1 byte.

What is meant here by

Base class subobjects are not so constrained.

is that the object itself won't consume any space rather it's members will. So the address of an object may match with that of its first member subobject, same as in case of arrays. Thats the optimization.

While in case of derived class, it will necessarily include the base class object, which even empty will consume 1 byte, hence above optimization is not valid for a derived class.

Hemant