Structures in C programming language are collection of different data types. Click here for more details about structures. C compiler will structure padding for the structures in the memory alignment..
Why structure padding: Structure padding will be used for fast execution. Generally memory allocation will be done basing on the size of the int for the fast execution. So to achieve this rule, if the size of the memory space is less than the size of the int (for example char it takes 1 byte), padding will be done by adding required bytes.
Lets look at the below example: In the example, structure contains char (1byte) and int(4 bytes), so total size should be 5 bytes. But because of memory alignment for fast execution, compiler will be added extra 3 bytes as a padding, and the total size of the structure will be 8 bytes.
Sample C program for Structures:
Output:
$ ./a.out
size is 8
Output:
$ ./a.out
size is 8
Look at another example: In the below example, we added the extra char c2 after the int variable and the size of the structure is now 12 bytes. This is also due to padding. char+int+char which is equal to 1+3(padding)+4+1+3(padding).
In the above example, two chars and int takes 8 bytes, but here two chars and int takes 12 bytes. This is because of order of the variables used in the structure. For structure padding, order of the variables are also very important.
So in the below example, first char 1byte and no chars are there after it, so 3bytes for padding, for int b bytes and for another char c2 one byte and 3 padding bytes. If another char is there after c2, only 2 bytes padding will be required.
Output:
$ ./a.out
size is 12
Why structure padding: Structure padding will be used for fast execution. Generally memory allocation will be done basing on the size of the int for the fast execution. So to achieve this rule, if the size of the memory space is less than the size of the int (for example char it takes 1 byte), padding will be done by adding required bytes.
Lets look at the below example: In the example, structure contains char (1byte) and int(4 bytes), so total size should be 5 bytes. But because of memory alignment for fast execution, compiler will be added extra 3 bytes as a padding, and the total size of the structure will be 8 bytes.
Sample C program for Structures:
#include<stdio.h> struct Sample{ char c; int n; }; int main() { struct Sample s; printf("size is %u\n",sizeof(s)); }
Output:
$ ./a.out
size is 8
Look at another example: In the below example we added extra char variable c2, still the size of the structure is same. Because char takes one byte and two chars are in sequential, so size is (1+1+2padding+4), so total size is 8 bytes.
#include<stdio.h> struct Sample{ char c1; char c2; int n; }; int main() { struct Sample s; printf("size is %u\n",sizeof(s)); }
Output:
$ ./a.out
size is 8
In the above example, two chars and int takes 8 bytes, but here two chars and int takes 12 bytes. This is because of order of the variables used in the structure. For structure padding, order of the variables are also very important.
So in the below example, first char 1byte and no chars are there after it, so 3bytes for padding, for int b bytes and for another char c2 one byte and 3 padding bytes. If another char is there after c2, only 2 bytes padding will be required.
#include<stdio.h> struct Sample{ char c1; int n;
char c2;}; int main() { struct Sample s; printf("size is %u\n",sizeof(s)); }
Output:
$ ./a.out
size is 12
No comments:
Post a Comment