Microcontroller Embedded C Programming Lecture 149| Calculating structure size manually with and without padding

  • Post author:
  • Post category:Blog

 

Calculating structure size manually with and without padding

 

In the previous article, you calculated the structure size including padding using a program. Now let’s learn how to calculate this manually.

Here is a diagram to analyze memory consumption. Let’s consider the same structure, and the width of this memory as 4 bytes, as shown in Figure 1.

Figure 1. Structure Padding
Figure 1. Structure Padding

 

Let’s start from 0. The first member element data1 is char, it is one byte. Data1 will be stored in the First room. 

Next member element data2 is int, int is of 4 bytes. But there is room for only 3 bytes. That’s why ‘int’ will occupy the next place. The remaining 3 rooms are padded with zeros, as shown in Figure 2.

Figure 2. Structure Padding
Figure 2. Structure Padding

 

After that next is char, char is 1 byte. char will occupy the next position (data3), as shown in Figure 3.

 structure size calculation
Figure 3. Structure padding

 

And next is short. short is 2 bytes. You cannot fit short after the data3. So, short will be moved to the next aligned memory locations, short is stored next location(data4), and the empty location will be padded with zero.

 structure size calculation
Figure 4. Structure Padding

 

You can understand the previous exercise output something like this. And you can theoretically calculate the size of the struct including padding. So, 4 + 4 + 4, which is 12 bytes.

 

What happens if you do no padding? 

To do no padding you have to attach the compiler attribute that is called packed → __attribute__((packed)); If you do that, padding will not be inserted.

Here, what happens is, the first is char which is 1 byte. Data 1 will be stored in the first memory location. 

The next member element is int, which is 4 bytes. It will be forced to use after the data1. Data2 is in the second byte, data2 is in the third byte, and data2 is in the fourth byte. Here we can see that there is a split of data2, which is not good if you consider the performance.

And after that next is char (1 byte), which is a place for data3, so one byte is sufficient, no problem.

Next, a short(2 bytes)is stored in the next memory location, data4. The packed structure(No padding) is shown in Figure 5. So, 4 + 4. In Total, you consume 8 bytes. 

 structure size calculation
Figure 5. Packed structure (No padding)

 

You can see that in a packet structure everything is packed, so no room for padding. 

In the following article, let’s do an assembly code analysis of packed and non-packed structure. 

 

Get Full Course on Microcontroller Embedded C Programming Here.

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.