What is the Size of an Empty Class

What is the logic behind size of an empty class = 1? (As per C++)
Ex:
class base {
};

main()
{
printf("Size of the Empty Class is: %dn", sizeof(base));
}

It gives output: Size of the Empty Class is: 1

I have not got the reason / logic behind this.
Can anyone help me?

Questions by navneetgoyal2000   answers by navneetgoyal2000

Showing Answers 1 - 9 of 9 Answers

chiranjit

  • Feb 23rd, 2011
 

The logic behind is... When we create a class in turbo C++ compiler a memory is being blocked for that class say 1 byte.. it gives the compiler a prior knowleadge that a class is being created and somethning is there inside the class. So the compiler shows 1 byte, but when we give some datatype inside the class it gives the size of the total datatype.

  Was this answer useful?  Yes

amitmanish

  • Mar 4th, 2011
 

The size of an empty class's object is 1.

The reason for this is that suppose if we have the size as 0, then two objects created one after the other would point to the same address. Having the size as minimum 1 effectively ensure that the addresses of two different objects will be always different.

  Was this answer useful?  Yes

nilesh2080

  • Mar 11th, 2011
 

The size of the empty class is i byte because empty class will provide the default constructor, default destructor, default copy constructor and default assigment operator. These function are called by the address of the pointer to functions, so the size of these address is 1 byte.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions