www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1995/10/31/03:06:49

Date: Tue, 31 Oct 1995 08:43:32 +0200 (IST)
From: Eli Zaretskii <eliz AT is DOT elta DOT co DOT il>
To: Ojas Parekh <gt7010a AT prism DOT gatech DOT edu>
Cc: djgpp AT sun DOT soe DOT clarkson DOT edu
Subject: Re: Determining proper sizeof a struct

On Mon, 30 Oct 1995, Ojas Parekh wrote:

> This program prints a value of 148 for sizeof(ulfrec) under both
> DJGPPV2 and DJGPP 1.12m4, but the value should be 146.
> Compiled under BC 3.1, this same program reports 146 for
> sizeof(ulfrec) as it should.  Is there something about gcc/DJGPP
> data types that I am not aware of?  Thanks.

Gcc (and other 32-bit compilers) pad structures so that they could be 
accessed faster by 32-bit processors (there is a penalty of accessing 
unaligned addresses).  It is usually wrong to assume that sizeof of a 
struct is the sum of the sizes of its fields; 16-bit DOS-based compilers 
just proliferate this bad habit by not padding structures, because it's 
much less important in 16-bit code.

If you need to make the structure packed (i.e. without padding holes), 
you should use the Gcc ``__attribute__ ((packed))'' extension (described 
in Gcc Info manual), but this should only be used when the structure must 
fit some external specification, like a specific data layout of a device 
driver or a hardware peripheral.

- Raw text -


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