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.
Answer Posted / 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 |
Post New Answer View All Answers
What is dev c++ used for?
C is to C++ as 1 is to a) What the heck b) 2 c) 10
What is c++ mutable?
Where the memory to the static variables is allocated?
What is the use of 'using' declaration in c++?
What is the use of class in c++?
What are the types of pointer?
Define macro.
What is the real purpose of class – to export data?
What is the hardest coding language to learn?
Tell me what are static member functions?
What is the use of object in c++?
What is the size of integer variable?
What is doubly linked list in c++?
write a corrected statement in c++ so that the statement will work properly. x = y = z + 3a;