Structure Alignment, Padding and Data Packing in C

Why Struct Members are Aligned?

In this article we will focus on member alignment within structures in C; but before that let's see why is data alignment important? All processors are architectured to achieve optimal performance and to make memory access faster. Aligning data elements allows the processor to fetch data from memory in an efficient way and thereby improves performance.

By definition, Alignment refers to the location of data in memory; means which address data should be stored at. A variable is naturally aligned if it gets stored at a memory address that is a multiple of its size. For example, a 32-bit long variable is naturally aligned if it is located in memory at an address that is a multiple of four. Alignment in C is defined as a "requirement that data of a particular type be located on storage boundaries with addresses that are particular multiples of a byte address".

Structure Alignment and Padding

Now that we know a variable stored in memory should be stored at the address divisible by its size; therefore, the random order of variables declared with in a structure can cause memory wastage. We can try to minimize this memory wastage by ordering the structure elements such that the widest (largest) element comes first, followed by the second widest, and so on. The following example helps illustrate the effect of ordering of data elements on the size of the structure. The following trivial C program I executed on a x86_64 Linux box, where you can see the order of members proves to be useful in optimizing the memory usage.

#include <stdio.h>
 
int main()
{
  //declare members in random order
  typedef struct
  {
   char ch1; short sh1;
   char ch2; float fl1;
   int in1; char ch3;
   double dbl1;
  }random_struct;
 
  //declare members from largest to smallest order
  typedef struct
  {
   double dbl1; float fl1;
   int in1; short sh1;
   char ch1, ch2, ch3;
  }ordered_struct;
 
 
  random_struct s1;
  ordered_struct s2;
  printf("Size of s1: %d\n", sizeof(s1));
  printf("Size of s1: %d\n", sizeof(s2));
}
 
OUTPUT
======
Size of s1: 32
Size of s1: 24

Last Word

As you can see above order of member declaration within a structure greatly helps reducing memory holes, this is because padding bits are left blank if any data member is not getting an address divisible of its size. Hope you have enjoyed reading this article. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!




Get Free Tutorials by Email

About the Author

is the main author for cs-fundamentals.com. He is a software professional (post graduated from BITS-Pilani) and loves writing technical articles on programming and data structures.

Today's Tech News

Robotic barman pours Rory a pintPosted on Friday March 24, 2017

The BBC’s technology correspondent Rory Cellan-Jones gets a beer poured for him by a robotic barman - but how long does it take?

Row over US ISP customer data salesPosted on Friday March 24, 2017

US politicians have voted to let ISPs gather and sell data about what Americans do online.

Ofcom plans instant payback for broadband woesPosted on Friday March 24, 2017

Customers who suffer poor service could get automatic payouts under Ofcom's plan.

Courtesy BBC News