Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Alignment and Padding in Structs | Understanding Structs and Memory
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Mastering C Structs

bookAlignment and Padding in Structs

Alignment

Each structure field is aligned to a boundary that is a multiple of the size of the structure's largest element.

For example, if the largest element is 4 bytes in size, then each element will be aligned on a 4-byte boundary.

This is done to speed up memory access and avoid hardware alignment problems.

main.c

main.c

copy
1234567891011121314151617181920
#include <stdio.h> // simple struct struct Example { char a; int b; double c; }; int main() { struct Example test; printf("Size of struct Example: %zu\n", sizeof(test)); printf("Address of test.x (char): %p\n", &test.a); printf("Address of test.y (int): %p\n", &test.b); printf("Address of test.c (double): %p\n", &test.c); return 0; }

You might be wondering why the size of struct Example is 16 bytes, even though each field is aligned to the boundary of the largest type, which is 8 bytes. At first glance, aligning all three fields to 8 bytes might suggest a total size of 24 bytes. In reality, it works a bit differently.

The first field a is a char and takes up 1 byte. To make sure the next field b of type int starts at a 4-byte boundary, the compiler adds 3 bytes of padding after a. The field b itself takes 4 bytes and is now properly aligned.

The next field c is a double and needs to start at an 8-byte boundary. The compiler adds padding after b to place c at the correct address. The field c occupies 8 bytes.

In total: 1 byte for a + 3 bytes padding + 4 bytes for b + 8 bytes for c = 16 bytes. The alignment rules are followed, and memory is used efficiently.

question mark

Why does the size of a struct often exceed the sum of the sizes of its individual fields?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain why the struct isn't 24 bytes if the largest element is 8 bytes?

How does padding work in structs with different data types?

Can you show an example of how the memory layout looks for this struct?

bookAlignment and Padding in Structs

Swipe to show menu

Alignment

Each structure field is aligned to a boundary that is a multiple of the size of the structure's largest element.

For example, if the largest element is 4 bytes in size, then each element will be aligned on a 4-byte boundary.

This is done to speed up memory access and avoid hardware alignment problems.

main.c

main.c

copy
1234567891011121314151617181920
#include <stdio.h> // simple struct struct Example { char a; int b; double c; }; int main() { struct Example test; printf("Size of struct Example: %zu\n", sizeof(test)); printf("Address of test.x (char): %p\n", &test.a); printf("Address of test.y (int): %p\n", &test.b); printf("Address of test.c (double): %p\n", &test.c); return 0; }

You might be wondering why the size of struct Example is 16 bytes, even though each field is aligned to the boundary of the largest type, which is 8 bytes. At first glance, aligning all three fields to 8 bytes might suggest a total size of 24 bytes. In reality, it works a bit differently.

The first field a is a char and takes up 1 byte. To make sure the next field b of type int starts at a 4-byte boundary, the compiler adds 3 bytes of padding after a. The field b itself takes 4 bytes and is now properly aligned.

The next field c is a double and needs to start at an 8-byte boundary. The compiler adds padding after b to place c at the correct address. The field c occupies 8 bytes.

In total: 1 byte for a + 3 bytes padding + 4 bytes for b + 8 bytes for c = 16 bytes. The alignment rules are followed, and memory is used efficiently.

question mark

Why does the size of a struct often exceed the sum of the sizes of its individual fields?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2
some-alt