Class size

When I create a class not having any data member or member functions and try to find its size it returns "1".

I just want to know its significance.
Who is Participating?
KangaRooConnect With a Mentor Commented:
According to the standard, even objects of an 'empty' classes should have a non-zero size. You can imagine that it must be possible to take the address of such an object. What address could that be if there is no 'real' object?

arnond, the size of a pointer is never 1, is it. Even small dos pointers were 2 bytes. Well, the 6510 (CBM64) processor could use 1 byte pointers ;)

From the standard (1997 Public Review)
9   Classes                                              [class]

3 A class with an empty sequence of members and base class objects is an
  empty class.  Complete objects and member subobjects of an empty class
  type  shall have nonzero size.1) [Note: class objects can be assigned,
  passed as arguments to functions, and returned  by  functions  (except
  objects  of  classes  for  which  copying  has  been  restricted;  see
  _class.copy_).  Other plausible operators, such as  equality  compari-
  son, can be defined by the user; see _over.oper_.  ]
  1) That is, a base class subobject of an empty class type may have ze-
  ro size.

before you add anything to the class, there's only a pointer to the class and this pointer's size is "1".

Arnon David.
This is compiler dependent.
What compiler are you using???
In VC++ 6.0 the following code gives the number of bytes as 4.
The 4 bytes is a pointer to the Virtual function Table which stores all the virtual functions.

      CDone      done;

      int            nDone = sizeof(done);

where CDone is my member less class.

The number of bytes basically depends on the internal datastructure used by the compiler for instantiating the class.

Feel free to ask
Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

> The 4 bytes is a pointer to the
> Virtual function Table which stores
> all the virtual functions.

an empty class does not have a vptr.  Simply speaking, any class that has no virtual methods has no vtable.  C++ classes that are plain old data structures (you can look up the rule on what constitutes a POD in C++) must be binary compatible with their 'C' counterpart, and that means no vptr.
rvgcppAuthor Commented:
Agreed. It is as per the standard.
But then I want to know (if possible) what is the reason behind this. Anywhere this info (i.e. its size 1) is used.
Even though you gave me a C here :( I could imagine some space is allocated because it must be possible to create a pointer to an object:

  class empty{};

  empty  object;
  empty* pointer = &object;

If you want to be able to do that the object must occupy some memory.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.