X86 Assembly Struct Padding

Hi Friends

I would like to write about struct padding. Struct padding is all about the alignment of the data in memory for read optimization.

As you may know that in 32 bit processor the memory read is optimized to read word or 4 bytes, so it is important to alight the data in memory to be placed in division of 4.

first we need to know each of data type size. You can refer to the picture below

for example of our code like below, so based on the data size above then we drill down the alignment as below

The size of stu_b is equal to 8 because of the below memory layout

The size of stu_c is also equal to 12 because of below memory layout

So we can see that the implementation of padding in struct impacting the size of the struct itself. So you can start manipulating the position of the field in the struct so that it gives you faster reading and smaller size of the struct.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s