Mail Archives: djgpp/1996/04/11/13:52:52
Mike <mbwarren AT acs DOT ucalgary DOT ca> writes:
>what am i doing wrong:
>with djgpp v2, when i take the sizeof this struct:
>typedef struct
>{
> char magic[2];
> long size;
> int dummy;
> long offset;
>} header_t;
>it returns 16, when it should return 14 (2 chars = 2 + long (4) + int (4)
>+ lont (4) = 2+4+4+4=14)
This argument is incorrect. The size of the struct should be
2 + P1 + 4 + P2 + 4 + P3 + 4 + P4
where P1,...,P4 are arbitrary non-negative integers. (This, of
course, assuming that longs and ints are fours bytes each.)
In short: the C standard does not define the memory layout of
a struct very precisely, but explicitly leaves it to the
implementation. For speed, djgpp aligns modulo 4. (It could
even be argued that two consequtive compiles needn't chose
the same layout, but that's not likely.)
Check the manual and the FAQ for ways of overriding this.
>(BTW, why are int's represented as 4 bytes on a PC? sizeof(int) returns
>4...)
Well, 2-byte-integers are too small for confort; 8-byte-integers are
rarely needed. Four is such a nice number.
Morten
- Raw text -