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.
rvgcppAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Arnon David.
0
KangaRooCommented:
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.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
abdijCommented:
Hi,
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.

Bye
Feel free to ask
Abdij
0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

jasonclarkeCommented:
> 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.
0
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.
0
KangaRooCommented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.