Why the size of empty Class is one byte?

Showing Answers 1 - 48 of 48 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

jadhav manjula

  • Feb 2nd, 2016
 

Class is user define data type it store 1 byte memory

  Was this answer useful?  Yes

Subhashish

  • Apr 26th, 2016
 

Since C++ Compiler allows to create an object of an empty class, so the minimum amount of memory required to hold the object is 1 Byte. thats why Empty size of empty class is 1 byte

  Was this answer useful?  Yes

ujjwal

  • May 9th, 2016
 

class A
{
int a[0];
};
output will be 0 bytes
in this case your theory will be wrong.
now B can Overwrite Class A

  Was this answer useful?  Yes

aaa

  • Jun 15th, 2016
 

The output of above code is 1 byte only...

  Was this answer useful?  Yes

raj

  • Oct 20th, 2016
 

I am getting 1 byte size without creating object for empty class.
sizeof(Empty);

  Was this answer useful?  Yes

Jagadeesh

  • Nov 14th, 2016
 

Output of above code is 0 byte not 1 byte .

  Was this answer useful?  Yes

narasimulu

  • Feb 16th, 2017
 

1 byte

  Was this answer useful?  Yes

purvi

  • May 9th, 2017
 

4byte

  Was this answer useful?  Yes

lokesh

  • Sep 16th, 2017
 

1 byte

  Was this answer useful?  Yes

NADEEM JAWED

  • Oct 10th, 2017
 

The size of empty class is 1 byte because every object has different address so the size of empty class is 1 byte

Code
  1. #include<iostream>

  2. using namespace std;

  3. class A{

  4.  

  5. }

  6. int main()

  7. {

  8.    A obj;

  9. cout<<sizeof(obj)<<endl;

  10. }

  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