Empty Class

LeelaSwamy
LeelaSwamy used Ask the Experts™
on
The following code:

Class Fred
{
};

main()
{
        Fred f;
        cout<<sizeof( f );
}

Result : 1

How?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
The C++ standard states that the minimum size for any class that can be instantiated (i.e. non-abstract class) is 1 byte, so compilers allocate 1 byte of space for it.
Just to clarify, 1 byte is allocated at the bottom level of the hierarchy.  If you have an empty class inherit from an empty class, it will still only allocate 1 byte (not 1 byte for each level).  I hope this helps.

Mike
You can find "Empty Classes" document in MSDN.

Some part mentioning here...

You can declare empty classes, but objects of such types still have nonzero size. The following example illustrates this:

#include <iostream.h>

class NoMembers
{
};

void main()
{
   NoMembers n;  // Object of type NoMembers.

   cout << "The size of an object of empty class is: "
        << sizeof n << endl;
}

This is the output of the preceding program:

The size of an object of empty class is: 1.

The memory allocated for such objects is of nonzero size; therefore, the objects have different addresses. Having different addresses makes it possible to compare pointers to objects for identity. Also, in arrays, each member array must have a distinct address.

Microsoft Specific

An empty base class typically contributes zero bytes to the size of a derived class.

END Microsoft Specific


//////////////////////////////////////////////////

Also find a document named "sizeof Operator" that also giving specification about that.


GOOD LUCK
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

As a final note, the main reason for this is that no two objects can share the same
address (from the C++ standard). This forces all objects to have a non-zero size
so that you can allocate an array of empty objects and each one has a unique
address. It's just an implementation reality and an edge case in the standard.

Commented:
LeelaSwamy,
You have posted a total of five questions, and all five questions are open, and have not been awarded points.

You have 5 questions open in the C++ topic area, some of which are two months old.

Please award points for your previous questions and closed them.

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: mnashadka {http:#9239709}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial