Hi Philip
The problem could be caused becoz of the use of sizeof. If you
see in the Linux source code also they do not rely on sizeof for reading
data from files. What they do is use #define to define a SIZE macro
defining the size of the struct and then read from file using that macro
as size
regards
Aditya
| 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.
|
| Check of memory addresses reports:
| Type[0]: bffffbb8
| Type[1]: bffffbb9
| Size: bffffbbc
| Res1: bffffbc0
| Res2: bffffbc2
| Bits: bffffbc4
|
| As you can see, the problem seems to be between Type and Size
| There seems to be two extra bytes allocated, presumably as padding for
| word boundaries or something like that. This of course is causing my
| whole program to go haywire because two bytes are being read into nowhere.
|
| I can read them with Type[2] and Type[3], but that is of no use to me as
| they have to be the lsbs of Size. Size in turn gets two bytes that belong
| to Res1 and so on.
|
| How can I force the compiler to not pad to word boundaries?
|
| Philip
|
|
| 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
|
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