delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/05/11/20:40:24

From: j DOT aldrich6 AT genie DOT com
Message-Id: <199605120016.AA213930191@relay1.geis.com>
Date: Sun, 12 May 96 00:17:00 UTC 0000
To: djgpp AT delorie DOT com
Mime-Version: 1.0
Subject: Re: more sizeof questions

Reply to message 8067707    from SLH100 AT YORK DOT A on 05/09/96  5:52AM


[snip]

>btw. this is one good reason not to dump structures into files just by
>calling fwrite(&struct, sizeof(struct). It's tempting to do, but it tends
>to break code when you move from one compiler to another...

There is a way to make sure that your code is portable on most machines.

1)  Never use 'int'; declare all your vars as short or long depending on how
many bytes you want.

2)  Conditionally pack the structure only if you are using a GNU compiler,
i.e.:

#if defined(GNUC)
} __attribute__((packed));
#else
};
#endif

These two steps should allow your program to generate structures that
can be saved in a binary format that is the same for just about any
compiler, 16-bit or otherwise.  I used this trick for a couple of class
projects
because I was using DJGPP at home and the lab computers (where I
printed out my data) used Turbo C.

BTW, doesn't Turbo C suck?  I had a program that used ftruncate in
<unistd.h>, only to discover that TC has no such library and no such
function.  I had to write two different sections of code, one for Turbo C
and one for real compilers.  :(

John

- Raw text -


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