delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/01/28/02:02:28

Date: Fri, 27 Jan 1995 23:12:50 -0500
From: davis AT amy DOT tch DOT harvard DOT edu ("John E. Davis")
To: djgpp AT sun DOT soe DOT clarkson DOT edu, hoffo002 AT gold DOT tc DOT umn DOT edu
Subject: RE: Major bug

>   struct {
>      char name[8];
>      long unused1;
>      word width;
>      word height;
>      long unused2;
>      word p_size;
>   } header;
>
>   fread(&header, sizeof(header), 1, fp);
>
>Then, I put in a debugging line like:
>
>   printf("%d\n", sizeof(header));
>
>Which gave me 24 for output.  If you look carefully, however, you will see 
>that it should be 22.  Why is it giving be 24?  Have no clue, but it must 
>be a bug.  I actually checked the sizeof() of each field in the header 

I am afraid that the bug is in *your* program.  You are depending upon the
non-portable behavior of the structure being packed.  As you have found out,
gcc pads it.  There might be some compiler switch to force the compiler to
pack the structure but I would not rely on it.  You are better off rewriting
parts of your program to remove such non-portable code.  While you are at
it, you should also worry about byte ordering.  Not all systems have the
same byte ordering as the pc.


- Raw text -


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