Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


what is the size of this class
class size
{
public:
char data1;
double d;
int data2;
char data3;
double data4;
short data5;
};

please explain the padding for these double variables.

Answers were Sorted based on User's Feedback



what is the size of this class class size { public: char data1; double d; int data2; char..

Answer / nivvy

This size is 32

Char : 1 + 3 bytes padding
Doube : 8 Bytes
int : 4 bytes
char : 1 + 3 bytes padding
double : 8 bytes
short : 4 bytes

so 32 bits

Is This Answer Correct ?    6 Yes 0 No

what is the size of this class class size { public: char data1; double d; int data2; char..

Answer / abhishek

absolutely correct navvy agreed !!

just a minor change

short : 2 bytes + 2 byte padding

Is This Answer Correct ?    4 Yes 1 No

what is the size of this class class size { public: char data1; double d; int data2; char..

Answer / kanthi

I have a small doubt.. isnt it based upon the underlying
platform on which this piece of code is run?
as far as my c++ knowledge goes, the size of each datatype
varies for each platform unlike java. this is the reason
why java ensures portability of code, while c++ doesnt.
please tell me if i am wrong...

Is This Answer Correct ?    4 Yes 1 No

what is the size of this class class size { public: char data1; double d; int data2; char..

Answer / ada

I tried sizeof() the class and the output is 40 bytes. I
think the padding maybe like this:

char data1 1+7 bytes
double d 8 bytes
int data2 4 bytes char data3 1+3 bytes
double data4 8 bytes
short data5 2+6 bytes

So totally 5x8=40 bytes

Is This Answer Correct ?    3 Yes 1 No

what is the size of this class class size { public: char data1; double d; int data2; char..

Answer / kanthi

Just as an extension to my point in the previous post...
http://www.cplusplus.com/doc/tutorial/variables.html

the section fundamental datatypes in the above article
substantiates my point...

Is This Answer Correct ?    2 Yes 1 No

what is the size of this class class size { public: char data1; double d; int data2; char..

Answer / siva

Hi guys why dont you test properly before posting answers???

the answer is 40. how it is padding is as shown bellow

char data1 1+7 bytes
double d 8 bytes
int data2 4 bytes
char data3 1+7 bytes
double data4 8 bytes
short data5 4 bytes

so 40 bits

padding intermes of char type only, not for int, double and
short.

also, padding depends one next member type.

also observe char member are not in sequence.if they are in
sequence all will use same size as first one.

example:

if above structure is as shown bellow

struct size{
char data1 //1+7 bytes
char data6 //uses above 8 bytes
char data7 //uses above 8 bytes
char data8 //uses above 8 bytes
double d //8 bytes
int data2 //4 bytes
char data3 //1+7 bytes
double data4 //8 bytes
short data5 //4 bytes
}

still its size is 40 byts.

here you guys can observe before char data8 double d is
declared thatsway char data8 is padding to 1+7 = 8 byts
if it is int d then char data8 is padding to 1+3 =4 byts

Is This Answer Correct ?    2 Yes 1 No

what is the size of this class class size { public: char data1; double d; int data2; char..

Answer / venkat

How the conpiler know what data type is nexe to the current
data type?
So when checking the current data type to allocate memory,
previous data type is considered for padding.

In general int should start on 4byte address i.e. end 2
bits should be 00 Ex: 0x2000,2004,2008,200C etc..

Like that double has to start or to be save d at an address
which has last 3 bits a 000. Ex: 0x2000,2008,2010,2018 etc.

Similarly short on 2 byte boundary.. i.e last bit should 0.

Now when compiler see a double, checks what's the previous
one and if it's Char adds 7 bytes pad, if it's short-adds
6bytes padd and if it's int adds 4bytes pad.

Finally, in some compilers the total structure length is
padded to 2 to the power. Ex: If total size of all elements
in a structure are 28, compiler gives 32bytes i.e 2 to the
power of 5 ehich greater than the requirement.

Is This Answer Correct ?    0 Yes 0 No

what is the size of this class class size { public: char data1; double d; int data2; char..

Answer / nomesh

double will occupies 8 bytes. so here two double data type
variables create then this class size is 21

char 1 b
double 8 b
int 2 b
char 1 b
double 2 b
short 1 b

Is This Answer Correct ?    2 Yes 5 No

what is the size of this class class size { public: char data1; double d; int data2; char..

Answer / lakshmi sahu

22

Is This Answer Correct ?    1 Yes 4 No

Post New Answer

More C++ General Interview Questions

write asingle linked list which read from two list & the do the following 1 sort the prime & nonprime num (prime should be less tn nonprime) 2 each node has a prime num followd by nonprime 3 add a new node into its sutable plce 4 erase the most three duplicated non prime num 5 find the least duplicated prime num

0 Answers   Care,


When we use Abstract Class and when we use Interface?where we will implement in real time?

0 Answers  


Describe linked list using C++ with an example.

0 Answers  


What are associate containers?

0 Answers  


What are the vectors in c++?

0 Answers  


without if else statement can be written ah

0 Answers  


Can a class be static in c++?

0 Answers  


why the size of an empty class is 1

4 Answers  


What is while loops?

0 Answers  


What is a null object in c++?

0 Answers  


What is runtime polymorphism in c++?

0 Answers  


What is the difference between #define debug 0 and #undef debug?

0 Answers  


Categories