On Tue, 17 Jan 2006, Joel VanderWerf wrote:

> ara.t.howard / noaa.gov wrote:
>
>>         def bar
>>           @mmap[@offset + Integer::SIZEOF, Float::SIZEOF].unpack("f").first
>>         end
>>         def bar= f
>>           @mmap[@offset + Integer::SIZEOF, Float::SIZEOF] =
>> [Float(f)].pack("f")
>>         end
>
> Doesn't this arithmetic assume that the C compiler is packing the fields
> of the struct? What if fields are aligned on 8 byte boundaries, for
> instance? I vaguely remember having some issues like this when porting
> from x86 to sparc. I guess you could add __attribute__((__packed__)) to
> the struct to be sure.

absolutely.  i figured it was beyond the scope of the post to get into that -
but really the file format would need to export the shape of the struct in
some sort of header.  to do this one would need to crawl the struct with a
'void *' and compute offsets from the address of the struct.

of course, this would about the point where one should pull out xdr or some
such.  in practice, however, one often needs to read binary data written by a
program beyond one's control and the unpack approach will work most of the
time - wouldn't launch rockets with it though!

cheers.

-a
-- 
strong and healthy, who thinks of sickness until it strikes like lightning?
preoccupied with the world, who thinks of death, until it arrives like
thunder?  -- milarepa