Structure padding is adding extra bits at the end of the structure, so that the structure completes the word boundary.

How structure padding done

Structure Padding is done to do memory alignment. As size of pointer is 4 bytes, if everything is organized by multiple of 4, that it will be easier and faster to calculate the address and processing them. As structures are used to club different size variables, compiler will align to 4 byte boundaries and for that it needs padding.

Structure padding is done by the compilers and this depends on the architectures. Some architectures cannot access the data which will be stored on the odd addresses or they may find difficult to access it. This is the reason for padding extra bytes.

Padding will be done by compiler to structure’s members and to the structure as a whole also. Compiler pads structure as whole because this allows each member of structure aligned in array of structures.

Structure padding example

Struct EX
{
Char a;
Short int b;
Char c;
Long d;
};

Due to structure padding this structure size is
12 bytes (a=1 + 1 padding=2 + b=2 + c=1 + padding=3 + d=4) insted of 8 bytes (1+2+1+4).

Structure padding in C
a
b
c
d
char(1)Padding(1)short int(2)char(1)Padding(3)long(4)

Packed structures

Some compilers provide #pragma to suppress padding or to make it packed to n number of bytes. Some provide keywords to do this.

#pragma pack(n)

The keyword __attribute__ allows you to specify special attributes of variables or structure fields. This keyword is followed by an attribute specification inside double parentheses

So a packed structure is a structure without padding.
Generally packed structures will be used
• to save space, and
• to format a data structure to transmit over network using some protocol (this is not a good practice of course because you need to deal with endianness).

Structure packing in GCC

The packed attribute specifies that a variable or structure field should have the smallest possible alignment one byte for a variable, and one bit for a field, unless you specify a larger value with the aligned attribute.

Here is a structure in which the field x is packed, so that it immediately follows a:

struct foo
{
char a;
int x[2] __attribute__ ((packed));
};

aligned (alignment)

This attribute specifies a minimum alignment for the variable or structure field, measured in bytes. For example, the declaration:

int x __attribute__ ((aligned (16))) = 0;

gcc supports an extension that allows structures or structure members to be marked with __attribute__((packed)), which tells gcc to leave out all padding between members. Sometimes you need this, when you want to make sure of the layout of a structure. For example, you might have something like

struct my_struct {
int field1;
char field2;
} __attribute__((packed));

Without the packed attribute, the struct will have padding between field1 and field2, and that’s no good if this struct is something that has to match hardware or be sent on a wire.

But adding __attribute__((packed)) goes further than just telling gcc that it should not add padding – it also tells gcc that it cannot make any assumptions about the alignment of accesses to structure members. And this leads to disastrously bad code on some architectures.

To see this, consider the simple code

struct foo { int a; };
struct bar { int b; } __attribute__((packed));