2

I read the following code from wikipedia

struct MixedData
{
    char Data1;
    short Data2;
    int Data3;
    char Data4;
}md;
printf("%u    %u    %u    %u", &md.Data1, &md.Data2, &md.Data3, &md.Data4);

The output is:-

268624    268626    268628    268632

I am unable to understand how padding is applied after each data member in the above code?

kevin gomes
  • 1,775
  • 5
  • 22
  • 30

2 Answers2

1

From here

first element is char which is one byte aligned, followed by short int. short int is 2 byte aligned. If the the short int element is immediately allocated after the char element, it will start at an odd address boundary. The compiler will insert a padding byte after the char to ensure short int will have an address multiple of 2 (i.e. 2 byte aligned).

This is how your struct looks like:

+-------+-------+-------+-------+
| Data1 |       |     Data2     |
+-------+-------+-------+-------+
|             Data3             |
+-------+-------+-------+-------+
| Data4 |       |       |       |
+-------+-------+-------+-------+

So padding from char to short is one byte =>

268624 to 268625 is Data1, from 268625 to 268626 padding to short. then 268626 to 268628 short no padding needed, all aligned to max type of the struct.

From the same reference

There is a way to minimize padding. The programmer should declare the structure members in their increasing/decreasing order of size

The way to improve your struct is to place char Data4; directly after char Data1; This way no memory will be wasted for padding:

struct MixedData
{
    char Data1;
    char Data4;
    short Data2;
    int Data3;

}md;

printf("%p    %p    %p    %p", &md.Data1,&md.Data4, &md.Data2, &md.Data3);

0x602270    0x602271    0x602272    0x602274

The basic assumption, that compiler is stick with natural alignment of int on x86

Dabo
  • 2,371
  • 2
  • 18
  • 26
  • why only 2 bytes are used before short not 4? – kevin gomes Feb 06 '14 at 08:48
  • but if I declare two consecutive `char` as done in your answer, then why padding is not needed? – kevin gomes Feb 06 '14 at 09:00
  • In that case your structure is aligned, and no padding needed.What padding did in your case? Added one byte after Data1 to align char to short. Data1 + Data4 = 2 char, 2 char = 2 bytes, short two byte aligned, no additional padding required – Dabo Feb 06 '14 at 09:01
  • but also `int` requires 4 bytes, so why 2 bytes are not added after `short` in my question? – kevin gomes Feb 06 '14 at 09:04
  • because you already added one byte of padding after Data1. Data1 + 1 byte of padding = 2 bytes, after those 2 bytes you have 2 bytes of short 2+2 = 4 => you aligned with your int – Dabo Feb 06 '14 at 09:08
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/46907/discussion-between-dabo-and-kevin-gomes) – Dabo Feb 06 '14 at 09:08
  • The padding is unspecified in standard. Different architectures do the padding differently, some allow the programmer to enforce certain padding, some don't. Your answer covers an unknown compiler on an unknown machine with unknown architecture, hence it provides no information at all. The OP did not specify these hence it is impossible to actually answer this question. Or rather the answer is always the same: "the compiler decided so". – Dariusz Feb 06 '14 at 09:33
  • @Dariusz "The compiler decided so" will explain nothing to OP. I will add that my assumption is that my explanation with respect to "natural alignment" – Dabo Feb 06 '14 at 15:01
0

How?

2 bytes for 1 byte char

2 bytes for 2 byte short

4 bytes for 4 byte int

? bytes for 1 byte char

The real question is: why?

And the answer to that question is here, structure padding and packing and here, sizeof != sum (fields)

Community
  • 1
  • 1
Dariusz
  • 21,561
  • 9
  • 74
  • 114