Re: C Program Portability

From: Flash Gordon (spam_at_flash-gordon.me.uk)
Date: 09/23/04


Date: Thu, 23 Sep 2004 22:18:34 +0100

On Thu, 23 Sep 2004 00:39:48 -0400 (EDT)
"Arthur J. O'Dwyer" <ajo@nospam.andrew.cmu.edu> wrote:

> On Wed, 22 Sep 2004, Flash Gordon wrote:
> >
> > "Arthur J. O'Dwyer" <ajo@nospam.andrew.cmu.edu> wrote:
> >>
> >> Avoid non-portable constructs. That is, avoid those constructs
> >> with implementation-defined, unspecified, or undefined behavior.
> >> Some examples include
> >>
> >> int i;
> >> fwrite(&i, sizeof i, 1, fp); /* bad! */
> >>
> >> unsigned char buf[sizeof (int)];
> >> buf[0] = i>>0;
> >> buf[1] = i>>8;
> >
> > Bad. What if CHAR_BIT!=8 ?
>
> Then we waste a few bits. I have absolutely no experience with
> porting data files between systems with different byte widths, but I
> imagine it might be easier and more useful to convert 9-bit bytes to
> 8-bit bytes by truncation, than to copy the exact bit pattern. In
> other words, in cases where it matters, this is a feature, not a bug.

I was thinking more of a 16 bit byte. Imagine this, you write your file
out on the 16 bit byte machine then transfer it to an 8 bit byte
machine. Now, since this is a binary file, any transfer is likely to
convert every 16 bit byte in to *two* 8 bit bytes, since to do otherwise
looses information. Then you have problems reading it!

Hence the comment I should have made (but may have forgotten to make)
that any code writing data as a binary file needs to be adapted for the
range of systems being used, and that the simplest way of dealing with
some possible systems is with different versions of the code.

> >> buf[2] = i>>16;
> >
> > Very bad, what if sizeof int <= 2 ?
>
> Hmm, yes, that is UB, isn't it? Better make that something like
>
> if (INT_MAX > 32767) {
> buf[2] = i >> 16;
> if ( [...???...] )
> buf[3] = i >> 24;
> else buf[3] = 0;
> }
> else buf[2] = buf[3] = 0;
>
> ...except that I can't think what to put in the [...???...] condition
> that would be strictly portable. Comparison to 8388607 isn't right.
>
> Of course, if I had taken my own advice and used 'long int' instead
> of 'int' in the first place (and 'unsigned long int' for any values
> that would need writing out to a file), then we wouldn't be having
> this conversation. :)

It's more that if you are dealing with reading, processing, writing a
binary format (say, doing some image processing on a greyscale image
with 16 bits per pixel, you might want to use different types on
different systems. If dealing with something less bulky, for instance
exporting a balanced ledger (financial data) from one system and
importing it to another (something I am involved in) you will probably
want to use a text format, since that can easily be produced and
easily be interpreted and translations between text formats can
generally be handled by the tool (FTP, for instance, which definitely
does line termination translation when told to transfer in text mode)
used to transfer the file.

-- 
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.


Relevant Pages

  • Re: Flash memory formating - Mac/Windows?
    ... For the format test, I used Disk Utility. ... 1:23.98 - immediately after writing the files ... 0:01.11 - the second time after the re-insert ... MS-DOS file system (FAT32) ...
    (comp.sys.mac.system)
  • Re: DVD / DVD+R /DVD-R / DVDRW. Are they all really distinct media
    ... lots of dusty disks: it rejects many once-used disks as unreadable. ... What's the best way to figure out what format my Laptop supports? ... Are these distinctions only relevant when writing disks at home as ... A little more complex is the difference between the '-' discs and '+' ...
    (microsoft.public.windowsxp.hardware)
  • Re: Build a better mousetrap ...
    ... I was recently involved in two books (technical reference, ... all writing is required to be submitted to the ... publisher in Microsoft Word format. ... How much less money are you willing to make to support ...
    (rec.arts.sf.written)
  • Re: Debug Assertion Failure
    ... I would read/write structure bytes to file *only* if it is a plain ... in these cases a finer serialization mechanics is required. ... because it's a text format, not a binary format), or using MFC ... Is 'offset' consistent with your binary file format? ...
    (microsoft.public.vc.mfc)
  • Re: Frustration: working with collaborators in Word..
    ... So, writing collaboratively ... As well, he's pressuring collaboration with ... I need to write in LaTeX, as I believe it's output is far superior to ... Convert back and forth into a word-compatible format (hopefully ...
    (comp.text.tex)