Series: Subject: Topic:
Question: 208 of 339

Why the size of empty Class is one byte?

Asked by: Interview Candidate | Asked on: Feb 16th, 2007
Showing Answers 1 - 6 of 6 Answers

Answered On : 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)

Login to rate this answer.

Answered On : Mar 22nd, 2007

View all answers by Gunvant

Yeah true. Just to mark its existence size is 1 byte.

Login to rate this answer.

Answered On : Jun 3rd, 2007

The handler consumes one byte if class is empty.

Login to rate this answer.

Answered On : 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.

Yes  1 User has rated as useful.
Login to rate this answer.

Answered On : Nov 27th, 2009

View all answers by a_l_soni

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.

Login to rate this answer.

Answered On : 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.

Login to rate this answer.

Give your answer:

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

Related Open Questions



twitter fb Linkedin GPlus RSS


Interview Question

 Ask Interview Question?


Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.