Alignment 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
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.
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
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?
Awesome!
Completion rate improved to 4.35
Alignment 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
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.
Thanks for your feedback!