www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/04/13/01:47:43

Xref: news2.mv.net comp.os.msdos.djgpp:2662
From: elric AT wheel DOT ucdavis DOT edu (Jeffrey Taylor)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: SIZEOF disparity...?
Date: 12 Apr 1996 16:29:49 GMT
Organization: Davis Community Network - Davis, California, USA
Lines: 36
Message-ID: <4km0dt$m38@mark.ucdavis.edu>
References: <Pine DOT A32 DOT 3 DOT 92 DOT 960406153743 DOT 75628A-100000 AT acs5 DOT acs DOT ucalgary DOT ca> <m2n34ijw64 DOT fsf AT presto DOT med DOT upenn DOT edu>
NNTP-Posting-Host: wheel.dcn.davis.ca.us
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Joe Smith (jes AT presto DOT med DOT upenn DOT edu) wrote:
: In article <4khbdr$1on AT vidar DOT diku DOT dk> terra AT diku DOT dk (Morten Welinder) writes:

[snip]

: Sigh.  Structures as file headers: just say 'no'.

Definitely! You may be able to outsmart one compiler, but another one 
will/may do it differently.  Also, the order of bitfields varies.  Some 
start packing from the most significant bit, others from the least.  
Moving between machines, byte order varies.  There are 24 (4 factorial) 
ways to store a 4-byte integer, 17 of them are in use by somebody.

For max portability, read into a byte array and convert to the struct 
yourself,  E.g.:

short a;
long b;
char buf[6];

read(fd, buf, sizeof(6));
a = (buf[0] << 8) | buf[1];
b = (buf[2] << 24) | (buf[3] << 16) | (buf[4] << 8) | buf[5];

This is most significant byte first.  You may choose differently.  If you 
are used to the Intel world, you might pick least significant byte first.

a = buf[0] | (buf[1] << 8);

Ugly, but it works on every platform I've tried.

--
============================================
Without my guitar, I am a poet without arms.
                        - Michael Bloomfield
============================================

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019