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

What is the object serialization?

0 Answers  


What kind of jobs can I get with c++?

0 Answers  


Search for: what is pair in c++?

0 Answers  


Can user-defined object be declared as static data member of another class?

0 Answers  


What is object in c++ example?

0 Answers  






Explain rethrowing exceptions with an example?

0 Answers  


What is vector string in c++?

0 Answers  


structure contains int, char, float how it behaves for big endian and little endian?

1 Answers   BITS,


What are the extraction and insertion operators in c++? Explain with examples.

0 Answers  


What is fflush c++?

0 Answers  


Explain differences between new() and delete()?

0 Answers  


What is an inclusion guard?

0 Answers  


Categories