GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 146 of 203    Print  
Why an empty Structure will occupy 2Bytes in C++

  
Total Answers and Comments: 5 Last Update: August 11, 2009     Asked by: pushpakumar 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: mucdull
 
Because the class has no members (especially no virtual functions) its objects naturally should have 0 bytes. However the objects must be legally creatable because the class is not abstract (having no pure virtual functions) so there must be a way to address the objects. You cannot address something that does not take up any memory. Since the smallest addressable space is 1 byte, the compiler allocates 1 byte for the objects so to make them addressable.

To dwell on the topic, what do you think is the size of objects of this class?

class C { virtual void Go() {} };

Above answer was rated as good by the following members:
j_l_larson
September 26, 2007 10:55:17   #1  
sukhvir        

RE: Why an empty Structure will occupy 2Bytes in C++

Size of empty structure in C++ is 1 byte because compiler assign 1 byte for structure name.


 
Is this answer useful? Yes | No
November 16, 2008 02:37:46   #2  
mucdull Member Since: November 2008   Contribution: 6    

RE: Why an empty Structure will occupy 2Bytes in C++
Because the class has no members (especially no virtual functions) its objects naturally should have 0 bytes. However the objects must be legally creatable because the class is not abstract (having no pure virtual functions) so there must be a way to address the objects. You cannot address something that does not take up any memory. Since the smallest addressable space is 1 byte the compiler allocates 1 byte for the objects so to make them addressable.

To dwell on the topic what do you think is the size of objects of this class?

class C { virtual void Go() {} };

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
February 18, 2009 03:45:59   #3  
santhosh.kanchanapally Member Since: April 2008   Contribution: 10    

RE: Why an empty Structure will occupy 2Bytes in C++
struct Emp
{
};

int _tmain(int argc _TCHAR* argv[])
{
cout<<"The size of structure is :"<<sizeof(Emp);
getchar();
return 0;
}

output::
The size of structure is : 1

 
Is this answer useful? Yes | No
February 18, 2009 03:46:47   #4  
santhosh.kanchanapally Member Since: April 2008   Contribution: 10    

RE: Why an empty Structure will occupy 2Bytes in C++
Hence the size of an empty structure is 1 byte
 
Is this answer useful? Yes | No
August 08, 2009 20:54:35   #5  
dkuy1 Member Since: August 2009   Contribution: 1    

RE: Why an empty Structure will occupy 2Bytes in C++

The Standard C++ language definition says:

A class with an empty sequence of members and base class objects is an empty
class. Complete objects and member sub-objects of an empty class type shall have
nonzero size.

Most compiler give a size of 1.


cantrip . org/emptyopt . html This link explains in great detail why this is
and when it can have a different size if use as part of a class or struct:


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape