I have the following code snippet.
#include<stdio.h>
int main(){
typedef struct{
int a;
int b;
int c;
char ch1;
int d;
} str;
printf("Size: %d \n",sizeof(str));
return 0;
}
Which gives a conclusion as follows
Size: 20
I know that size is structurelarger than summing component sizes structuredue to padding added to satisfy memeory alignment constraints. I want to know how the decision was made about how many fill bytes should be added. What does it depend on? Does it depend on the processor architecture? And it also depends on the compiler? I am using a 64 bit processor and gcccompiler here. How will the output change if these parameters change. Please explain some example, please.
I know there are similar questions in StackOverflow, but they do not fully explain these memory alignment limitations.