0
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?

Parul Agrawal
  • 45
  • 1
  • 4
  • 2
    Three bytes of padding between `b` and `z` presumably. Why is that surprising? What where you expecting instead? Might help to mention which 64 bit operating system you are talking about (and which chip). – john Dec 31 '20 at 07:57
  • 1
    Seems like data is aligned to 16-byte boundaries. Check the size of the individual data types on your system. And maybe clarify what size you were expecting. Did you expect it to be 64 bytes (aligned to 32 bytes), because `Test2` is 32 bytes large? – Lukas-T Dec 31 '20 at 07:57
  • Because of alignment requirements. – Aykhan Hagverdili Dec 31 '20 at 08:02
  • The only real guarantee is that the size of a `class` or `struct` is at least the sum of the sizes of its members. Practically, compilers often introduce padding to ensure *each* member is aligned appropriately (roughly speaking, all basic types need to be aligned so their address is a multiple of their size, so a four-byte `int` will be aligned so its address is a multiple of four). This padding would explain what you see. – Peter Dec 31 '20 at 08:03
  • Please describe, if possible explain, what you expect instead of the results you observe. Also your questions title implies a special focus on the fact that `Test` contains `Test2`, but that does not seem to play a role in the questions body. Or to put it differently what values did you expect? (You could also state the sizes of all parts, of all used types in your environment.) – Yunnosch Dec 31 '20 at 08:24
  • @john I was expecting 19 bytes of padding, because size of z is 32 – Parul Agrawal Dec 31 '20 at 09:36

2 Answers2

0

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

User
  • 572
  • 2
  • 10
0

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.

  • You can use the alignof operator to tell each type's alignment.
  • You can use the old offsetof to tell what is the starting address of each member in your class.
SHR
  • 7,940
  • 9
  • 38
  • 57