delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1996/05/08/23:44:38

From: j DOT aldrich6 AT genie DOT com
Message-Id: <199605090326.AA246132377@relay1.geis.com>
Date: Thu, 9 May 96 03:15:00 UTC 0000
To: djgpp AT delorie DOT com
Mime-Version: 1.0
Subject: Re: Structure size

Reply to message 1438646    from TGTCGS AT TC0 DOT CH on 05/08/96  7:45AM


>The MSC compiled program tells me sizeof(struct header)=98 and the DJGPP
>compiled program tells me sizeof(struct header)=100.... Can someone help
>me out on this one ????

As a 32-bit compiler, gcc aligns structures and individual fields of structures
on 4-byte boundaries.  98 is not divisible by 4, so it is rounded up to 100.
To
fix this, put the following at the end of your struct definition:

struct header {
 [stuff]
    } __attribute__ ((packed)) *head;

The __attribute__ ((packed)) tells gcc to align all the fields of the struct
and
the struct itself on 1-byte boundaries, i.e., "packed".

For more details on this, I suggest you look in the info docs for gcc, as it
not only explains all the different __attribute__ declarations, but tells you
the full reasoning behind 4-byte alignment.  Suffice it to say that in a 32-bit
environment, operations with word-aligned vars are far, far faster than with
non-aligned.

P.S.:  If you are using C++, the above will not work correctly due to a bug in
gcc.    To make it work, you have to surround your code with #pragma pack(1)
and #pragma pack().

John

- Raw text -


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