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
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 |
Answer / abhishek
absolutely correct navvy agreed !!
just a minor change
short : 2 bytes + 2 byte padding
Is This Answer Correct ? | 4 Yes | 1 No |
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 |
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 |
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 |
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 |
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 |
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 |
Write a String class which has: 1) default constructor 2) copy constructor 3) destructor 4) equality operator similar to strcmp 5) constructor which takes a character array parameter 6) stream << operator
2 Answers HCL, Lehman Brothers, Zoomerang,
Define the process of handling in case of destructor failure?
What is c++ library?
What is a singleton c++?
Can I learn c++ as my first language?
What is microsoft c++ redistributable 2013?
Write a c++ code that will calculate the roots of a quadratic equation a2+ bx+c=0 Hint: d = sqrt (b2-4ac), and the roots are: x1 = (€“b + d)/2a and x2 = (€“b €“ d)/2a (use sqrt function from cmath.h )?
What is the function of I/O library in C++ ?
What is the Maximum Size that an Array can hold?
55 Answers Adobe, FutureSoft, HCL, Infosys, Satyam, TCS, Wipro,
Which is not an ANSII C++ function a) sin() b) tmpnam() c) kbhit()
Can I learn c++ in a week?
What do you mean by function pointer?