At many places, it has been mentioned that in C, a structure (when not
packed) pads its content so as to facilitate memory alignment so that
its members are accessed faster.
like for e.g the snippet
struct node
{
char d;
int a;
};
int main(){
struct node a;
printf("size = %d",sizeof(a));
}
gives the output 8 as it pads the char to 4 bytes.
But the following snippet outputs 4
struct node
{
char d:1;
int a:1;
};
int main(){
struct node a;
printf("size = %d",sizeof(a));
}
I thought that maybe it was padding upto the nearest multiple of
sizeof(int), but then the following snippet gave the output 1.
struct node
{
char d;
};
int main(){
struct node a;
printf("size = %d",sizeof(a));
}
Can someone please explain, how is the whole padding thing happening??
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.