Saturday, October 1, 2011

Coding Puzzle No. 12?

#include<stdio.h>

struct a
{
    int s;
    char ee;
    int se;
    short r;
} sd;     

struct b
{
    int s;
    int se;
    char ee;
    short r;
} sds;


void main()
{
    printf("%d %d\n",sizeof(sd),sizeof(sds));
}




Output:
16 12


ConCept(tested in gcc complier):
At a time, 4 bytes are allocated for any data type. In 1st structure 4 bytes are allocated for short & char both, since they are allocated non-consecutively, whereas in 2nd structure memory allocation for both takes place within the same 4 bytes. 


No comments:

Post a Comment