In message <d0*************@news.t-online.com>
copx <in*****@invalid.com> wrote:
Hi, I remember hearing that the size of a struct can be bigger
than the sum of the sizes of its contents. Is that true?
For example let's say char is 1 byte and int is 4 byte in this case
and I've a struct defined like this:
struct pak_directory_entry_struct {
char file_name[56];
int file_position;
int file_length;
};
Is the size of this struct guaranteed to be 64 bytes
in this case or not?
It's definitely not guaranteed. But in this particular case, the vast
majority of real-world implementations with 32-bit ints would have the
structure as 64 bytes long. An awful lot of real-world portable code relies
on cases like that (particularly from BSD Unix), so an implementation that
wilfully added padding would be unlikely to be accepted by the market.
A more likely implementation difference will be the endianness of the ints.
BSD code tends to rely on the structure layout for file/network interchange
but uses endianness swap functions to sort out shorts and ints.
It's safer if you wrote data structures out to disc manually, one byte at a
time. A handy subroutine would be a "fput_int32" function that outputs a
32-bit number as 4 bytes.
--
Kevin Bracey, Principal Software Engineer
Tematic Ltd Tel: +44 (0) 1223 503464
182-190 Newmarket Road Fax: +44 (0) 1728 727430
Cambridge, CB5 8HE, United Kingdom WWW:
http://www.tematic.com/