Philip S Tellis forced the electrons to say:
> I've got a C program that has to read data from a file into a structure.
> There seems to be some problem with the way the structure is stored in
> memory.
>
> The structure format is:
>
> struct head {
> char Type[2]; /* 2 bytes */
> long Size; /* 4 bytes */
> short Reserved1; /* 2 bytes */
> short Reserved2; /* 2 bytes */
> long Bits; /* 4 bytes */
> };
> Add that up, you get 14 bytes.
> sizeof(struct head) reports 16 bytes.
This is because of the alignment that the C compiler forces on the structure.
Basically, it may require that a long _should_ start at the beginning of a
word, which is 4 bytes in linux. So there are two pad bytes added to the end
of the 2 byte array.
There might be a gcc extension to force this, but I am not aware of any. In
any case, code which needs to tell the compiler to change its alignment rules
need to be rethought.
If you use this structure to fread()/fwrite(), you don't have to worry about
the alignment - that is totally internal to the compiler, and transparent from
the user.
Binand
--
#include <stdio.h> | Binand Raj S.
char *p = "#include <stdio.h>%cchar *p = %c%s%c; | This is a self-
int main(){printf(p,10,34,p,34,10);return 0;}%c"; | printing program.
int main(){printf(p,10,34,p,34,10);return 0;} | Try it!!
To subscribe / unsubscribe goto the site www.ilug-bom.org ., click on the mailing list
button and fill the appropriate information
and submit. For any other queries contact the ML maintener