Click here to Skip to main content
15,883,870 members
Please Sign up or sign in to vote.
4.33/5 (3 votes)
See more:
Why size of empty class is 1 byte?
Posted
Updated 5-May-11 6:55am
v2
Comments
NuttingCDEF 5-May-11 12:47pm    
What's the problem? Why do you need to worry?
sadanandkenganal 8-Feb-12 12:59pm    
I din't get ,why exactly the empty class has 1 byte of data? PLz answer me ..
CPallini 13-Mar-13 5:47am    
It is for mitigating the empty objects identity crisis.

hello dear
i think the size of the Class very much depends on the Operating system or the compiler you are using to compile the class. On that basis the size of the class will be different for different OS or the compilers
as in some cases it will be 1 byte (may be in 64 bit environment) and some times it will be shown as 2 or 4 bytes and also it depends on the way you are trying to calculate the size of the class as if you are using the sizeof operator then the sizeof() operator can return at least 1 byte data not 0 bytes.

MSIL
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. Consider:
    class Empty { };

    void f()
    {
        Empty a, b;
        if (&a == &b) cout << "impossible: report error to compiler supplier";

        Empty* p1 = new Empty;
        Empty* p2 = new Empty;
        if (p1 == p2) cout << "impossible: report error to compiler supplier";
    }
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";
    }
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".



Origional post's URL for the above code : http://www2.research.att.com/~bs/bs_faq2.html#sizeof-empty[^]

also here i am embeding a URL of the different website. Is that OK
if i am not suppose to do that in that case let me know.

GOD Help Programmers/Developers.
Best regards
 
Share this answer
 
v2
Comments
NuttingCDEF 5-May-11 14:42pm    
Good answer - my 5.
Sergey Alexandrovich Kryukov 5-May-11 15:32pm    
Hope for GOD but tie your camel. Especially if you are a Developer. :-) My 5.
--SA
Because thats the minimum amount memory which can be allocated by system.
 
Share this answer
 
Comments
Emilio Garavaglia 5-May-11 17:05pm    
The system can perfectly allocate nothing!
The problem, here, is that C++ consider object addresses as the object identities. And to ensure two distinct objects will always have different identity, it conventionally assumne "1" as a minimum value for an object size.
But this is a C++ issues (depends on the way C++ defines the concept of "objects"), not a "system"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900