1

I was going through old examns and found this question, where I have to put in field size and padding size for this specific struct on a 64 bit operating system:

struct mystruct {
    char a;
    uint32_t b;
    int16_t c;
    int64_t d;
};

The answer is:

struct mystruct {
    char a;     //field size: 1, padding size: 3
    uint32_t b; //field size: 4, padding size: 0
    int16_t c;  //field size: 2, padding size: 6
    int64_t d;  //field size: 8, padding size: 0
};

I do understand why int16_t gets allocated 2 Bytes and 6 padding, because of the 64 bit architecture. Same with int64_t.

But why is the char allocated with 3 padding size and uint32_t with field size of 4 when its a 64 Bit architecture ?

IrAM
  • 1,720
  • 5
  • 18
paperplan3
  • 57
  • 5
  • This is strongly compiler and ABI specific. There cannot be a universal answer! Things could be different on Windows/x86-64 and Linux/PowerPC (even if you use some [GCC](http://gcc.gnu.org/) compiler). Some compilers even did rearrange `struct` members in some optimization passes. – Basile Starynkevitch Jan 04 '21 at 07:27
  • 1
    `I do understand why int16_t gets allocated 2 Bytes and 6 padding, because of the 64 bit architecture` - it is not because of the 64-bit architecture. It's because `d` is `int64_t` which had to be aligned at the 8-byte boundary, so a padding of 6 had to be added to `c`. If `d` was `int32_t d;`, then `c` would have field size: 2, padding size: 2. – GSerg Jan 04 '21 at 07:33
  • Okay, in the lecture we always used linux and i guess the were no optimizations like rearranging, except for the compiler recognizing the possibility of "splitting" the first 64 bit adress to fit in the char and uint32_t. – paperplan3 Jan 04 '21 at 07:37
  • @GSerg oh okay that makes sense! – paperplan3 Jan 04 '21 at 07:38
  • 1
    https://stackoverflow.com/a/38144117/477878 may be helpful. – Joachim Isaksson Jan 04 '21 at 07:38
  • @JoachimIsaksson thanks a lot, combined with the answer from kiran biradar it makes sense now for me! – paperplan3 Jan 04 '21 at 07:43

1 Answers1

2
struct mystruct {
    char a;     //field size: 1, padding size: 3
    uint32_t b; //field size: 4, padding size: 0
    int16_t c;  //field size: 2, padding size: 6
    int64_t d;  //field size: 8, padding size: 0
};

I do understand why int16_t gets allocated 2 Bytes and 6 padding, because of the 64 bit architecture. Same with int64_t. But why is the char allocated with 3 padding size and uint32_t with field size of 4 when its a 64 Bit architecture ?

Because:

  • char would start from any offset.

  • unit32_t would start from the offset mod(4) == 0.

  • int16_t would start from the offset mod(2) == 0.

  • int64_t would start from the offset mode(8) == 0.

thus

 offset  ->   0   1           4    8   10           16            24
              +--------------------+----------------+-------------+
              | a | 3byte pad |  b | c | 6byte pad  |   d         |
              +--------------------+----------------+-------------+
kiran Biradar
  • 12,700
  • 3
  • 19
  • 44