Hi,
I have little idea of padding.
i read in one of the links that :
all character variables gets stored in the memory location that is divided by 1.
all short variables gets stored in the memory location that is divided by 2.
all int variables gets stored in the memory location that is divided by 4.
so
struct one {
char ch;
}O;
printf("%d", sizeof(O));
is 1 byte ( no padding here )
struct two {
short s;
}T;
printf("%d", sizeof(T));
is 2byte ( no padding here also )
slly for only one integer 4 bytes.
now
struct Test {
char ch;
int i;
}Te;
printf("sizeof(Te)= %d\n",sizeof(Te));
here i will get out put as 8.
the reason is after storing the character variable next three bytes will be padded and after the i will be stored.
even if we write int i;
first and char ch;
second
the padding will be after the ch because when we create array of structures it will be fast to access.
my doubt is :
when the memory is allocted for char it can store any where regardless of memoey location because any number can be divided by 1.
say if char stores at location 2000.
next three bytes( 2001, 2002,2003 are padded ) and int will be stored at 2004.
but if char gets location 2001 or 2002 or 2003 then the memory for int should start at 2005,2006,2007 respectively.
then these numbers are not divisible by four.
i tried a program to check for this :
but i always got the first location that is divisible by four.
whether the first element is a char , short , int.
i never got any other location.
is there any reason that
always the first member of the structure stores at location that is divisible by four.
so that the rest will be easy to analyse.
Thanks,
Danian.