Why the size of empty Class is one byte?

Showing Answers 1 - 6 of 6 Answers

swetha

  • Feb 20th, 2007
 

creating the object means creating the memory, therefore even though there are no
members in the class also, it will create a phsical memory location of 1 byte and gets the address(object is reality)

  Was this answer useful?  Yes

pulak

  • Jun 3rd, 2007
 

The handler consumes one byte if class is empty.

  Was this answer useful?  Yes

ransome

  • Oct 18th, 2007
 

Yes, the compiler will generate 1 byte of memory to mark the existence of the class.  This doesn't answer WHY though.  The reason is the language standard states that all classes must have a memory size of at least 1 byte so that the class doesn't occupy the same memory space with another class.  This is to prevent name mangling.  i.e., if I declare a class A {};, the compiler will still generate an entry in its table to something called "A".  If behind that I declare another class, say class B, if A takes 0 bytes of memory, and B's data gets written in the place where A was declared.  In this case, an instantiation of A would take on the properties of B.

a_l_soni

  • Nov 27th, 2009
 

As per MSDN: "The sizeof operator never yields 0, even for an empty class."

When you calculate the size of a empty class that time you can c it takes 1 byte.which is the size of a char which a compiler takes for storing the address of a empty class.

The size of an empty class would depend on, what is the range specified for the compiler for char. It takes that much space to store the address of a class.

  Was this answer useful?  Yes

bhiku

  • Oct 10th, 2012
 

Yes, but we can put in other way also as we all know we have some default memfunctions created by compiler they are default constructor, default destructor, default copyactor, default assignment operator. So when you create a an object of empty class it will call the default constructor and look for the members to initialize if it wont find the members also first as soon u create object actor get called so whenever u create object it allocate space for class in memory so that will be default of 1byte if it finds members to initialize then it will combine 1 byte and
extend with that.

  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.

Answer Question

Click here to Login / Register your free account


 
Send   Reset

 

Related Answered Questions

 

Related Open Questions