[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Programming question: sizeof struct?

On Fri, Jul 09, 1999 at 09:09:15AM +0200, Joop Stakenborg wrote:
> The reason why I have not released LogConv 1.54 for Linux is that I am
> having problems with packed structures that is causing some file formats
> to not be handled properly.

Reading and writing structs to files is at best unportable, it is generally
considered bad design - and it is usually a broken method.  When handling
binary files you must think of the length of any specific field (int is not
necessarily 16 bit or 32 bit or 64 bit, for example!) and you must handle
bytesex.  These things CANNOT be reliably handled with structs without
kluging around.

NEVER read/write C data types to/from a binary file.  There are exceptions,
of course, but they are rare.

You should write a reader/writer that treats the file as an octet stream and
parses the data from it.

> So it's obvious that the compiler is
> placing a pad byte between text and num to align num.  I want it to
> stop!

You want your C compiler to compile something other than C.  In C, the only
guarantee you get is that the struct elements are stored in order (IIRC).

%%% Antti-Juhani Kaijanaho % gaia@iki.fi % http://www.iki.fi/gaia/ %%%

   "... memory leaks are quite acceptable in many applications ..."
    (Bjarne Stroustrup, The Design and Evolution of C++, page 220)

Reply to: