www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/06/26/01:28:18

Date: Wed, 26 Jun 1996 08:23:37 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: j DOT aldrich6 AT genie DOT com
Cc: djgpp AT delorie DOT com
Subject: Re: Help -- Reading PCX Header
In-Reply-To: <199606260356.AA228101373@relay1.geis.com>
Message-Id: <Pine.SUN.3.91.960626081531.7180C-100000@is>
Mime-Version: 1.0

On Wed, 26 Jun 1996 j DOT aldrich6 AT genie DOT com wrote:

> If you don't want to bother reading all that, the way to fix your problem
> is to change your struct definition as follows:
> 
> typedef struct {
>     ....
>         } __attribute__((packed)) pcxHead;
> 

Note that this is still not enough to ensure that the structure will be
read from a file without breaking a program, because (AFAIK) this doesn't
prevent the compiler from padding the struct *at the end* (in case
somebody makes an array of such structs).  If that happens, there will be
no holes between the fields, but the size of the struct (as returned by
sizeof) will still be larger than the sum of the sizes of all the fields,
so passing that size to `read' might still cause trouble. 

The only way to make this always work is to read the portion of the file
into a char of suitable length, then fill the struct fields one by one. 
If you do this, you don't need to make the struct packed, so you don't pay
the runtime penalty.  (Sometimes making the program correct also makes it
faster ;-). 

- Raw text -


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