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

Which software is used to run c++ program?

0 Answers  


What is the Diffrence between a "assignment operator" and a "copy constructor"?

3 Answers   Wipro,


How compile and run c++ program in turbo c++?

0 Answers  


how to find the maximum of 10 numbers ?

5 Answers  


A milk carton can hold 3.78 litres of milk. Each morning, a dairy farm ships cartons of milk to a local grocery store. The cost of producing one litre of milk is $0.38, and the profit of each carton of milk is $0.27. Write a C++ program that prompts the user to enter the total amount of milk produced in the morning. Then display the number of milk cartons needed to hold milk, the cost of producing milk, and the profit for producing milk.

2 Answers  






What is the difference between Class and Structure?

40 Answers   HP, IBM, Samsung, TCS,


What is c strings syntax?

0 Answers  


What is problem with Runtime type identification?

2 Answers  


In C++ cout is: a) object b) class c) something else

11 Answers   Infosys, Lehman Brothers,


What is virtual methods?

0 Answers  


What is buffering in c++?

0 Answers  


sizeof - is it a function or operator?

6 Answers   Honeywell,


Categories