• admin@embedclogic.com

Structure Packing, Alignment and Padding

Structure Size

To understand structure padding and alignment we have to go through the following examples-

Guess the size of below structures(32-bit machine)?

Expected Output:

Actual Output : 

Reason to differ actual output from expected is none other than Padding and padding required because of alignment.

Alignment is not a cosmetic modification or language requirement but it’s a necessary feature due to processor architecture to save the execution cycle.To get better understanding see memory alignment.

Memory Alignment:

A general purpose processor has a separate peripheral unit to store data, known as Memory.memory is basically word aligned and word size depends on address bus.Address bus is use to transfer address request from CPU to Memory while Data Bus is use to read or write data from/to requested address of memory.

Example:

For 16-bit processor–> Address bus will be 16 bit–>word length = 16-bit.

For 32-bit processor–> Address bus will be 32 bit–>word length = 32-bit.

For 64-bit processor–> Address bus will be 64 bit–>word length = 64-bit.

So lets see what does address bus length make sense for us –

Address bus length 16-bit/32-bit/64-bit means

number of Machine cycle required=data length/address-bus length(in bits)

Example:

Number of machine cycle required to process 32-bit data?

number of Machine cycle required(16-bit)=32/16= 2 machine cycle

number of Machine cycle required(32-bit)=32/32= 1 machine cycle

number of Machine cycle required(64-bit)=32/64= 1 machine cycle

Data Alignment 

So I think Now you can understand the meaning of the term word alignment.

Data alignment is the process in which processor store variables’s data into memory in such a way so that maximum data transfer efficiency can be get.

Padding is the process of insertion of reserve bytes to make every datatype word aligned in structure.

Here is the table to get aligned address :

Data TypeSize(32-bit)Storage Base Address
(for Alignment)
char1Anywhere
short int2Base address must be divisible by 2
int4Base address must be divisible by 4
long8Base address must be divisible by 4
float4Base address must be divisible by 4
double8Base address must be divisible by 8

Data Padding

Padding is the process of insertion of reserve bytes to fill the gap between the structure member cause due to alignment.

Now let’s understand above example :

A.

Explanation:

AddressMember Comment
0x00000000 xchar data can store anywhere in Memory
0x00000001padded_byte[0]int member can not stored here as it is not divisible by 4
0x00000002padded_byte[1]int member can not stored here as it is not divisible by 4
0x00000003padded_byte[2]int member can not stored here as it is not divisible by 4
0x00000004yint member can stored here as it is divisible by 4
Total Size 8 byte1(x)+3(padded)+4(y)

B.

Explanation:

AddressMember Comment
0x00000000 xchar data can store anywhere in Memory
0x00000001padded_byteshort int member can not stored here as it is not divisible by 2
0x00000002yshort int member can stored here as it is divisible by 2
Total Size 4 byte1(x)+1(padded)+2(y)

C.

Explanation:

AddressMember Comment
0x00000000 xchar data can store anywhere in Memory
0x00000001-
0x00000007
padded_byte_0[0]-padded_byte_0[7]double member can not stored here as it is not divisible by 8
0x00000008-
0x0000000F
ydouble member can stored here as it is divisible by 8
0x00000010-
0x00000011
zshort int member can stored here as it is divisible by 2
0x00000012-
0x0000001F
padded_byte_1[0]-padded_byte_1[5]To Maintain alignment in case of array of structure
Total Size 24 byte1(x)+7(padded_0)+8(y)+2(z)+6(padded_1)

D.

Explanation:

AddressMember Comment
0x00000008-
0x0000000F
ydouble member can stored here as it is divisible by 8
0x00000010-
0x00000011
zshort int member can stored here as it is divisible by 2
0x00000012xchar store anywhere
0x00000013-
0x0000001F
padded_byte_0[0]-padded_byte_0[4]To Maintain alignment in case of array of structure
Total Size 24 byte8(y)+2(z)+1(x)+5(padded_1)

Data Packing

.As we can see padding does the lots of memory waste whether it increases the performance.Some of the processor does much focus of memory size rather than its performance then they restrict alignment or use methods like #pragma to decide the own alignment boundary to reduce padding effect.This customize packing of data into memory is known as data packing.

Use of #pragma

Syntax:

#pragma pack (alignment_byte)

let’s see above example with pragma pack –

Output

So as you can see every structure member is aligned at 2-byte boundary.

You can also check with other alignment boundaries like 1 ,4,byte etc.

 

Subscribe and stay updated with our latest articles.