class Test2
{
char pq;
long double qr;
};
class Test
{
double c;
int a;
char b;
Test2 z;
};
sizeof(Test2)=32 sizeof(Test)=48
Why sizeof(Test) is 48 on a 64-bit operating system?
class Test2
{
char pq;
long double qr;
};
class Test
{
double c;
int a;
char b;
Test2 z;
};
sizeof(Test2)=32 sizeof(Test)=48
Why sizeof(Test) is 48 on a 64-bit operating system?
It's correct ,
The size of class Test2 is 32 because of the max size variable you have is long double
char 1 byte long double is 16 byte
so memory allocation is like
-----------------------------------------------------------
char 1 byte |.....padding 15 bytes | long double 16 bytes |
-----------------------------------------------------------
total 32
for class Test the largest size variable is of 8 bytes
so memory allocation is
-------------------------------------------------------------------------------------
double 8 byte |4 byte int then 1 byte char | padding 3 bytes | 32 byte Test2 object
-------------------------------------------------------------------------------------
total 48
It is all about memory alignment.
alignment uses to ensure types don't slip from one memory page to another. the compiler ensures the start address of a type is divided by its alignment.
For example: if a type has an alignment of 2 it can't start on an even address if it has an alignment of 4, the start address the last number must be 0,4,8 or c (hexadecimal). and so on, for alignment of 16, the start address (hex again) is always with 0 at the end.
Each type may have a different alignment.