delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/06/17/10:11:18

From: "John M. Aldrich" <fighteer AT cs DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: The size of structures and binary files
Date: Fri, 13 Jun 1997 19:21:56 +0000
Organization: Two pounds of chaos and a pinch of salt
Lines: 32
Message-ID: <33A19DD4.16BA@cs.com>
References: <33A1B030 DOT 309E AT post DOT psychologie DOT uni-bielefeld DOT de>
Reply-To: fighteer AT cs DOT com
NNTP-Posting-Host: ppp110.cs.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

News Administrator wrote:
> 
> Dear DJGPP users!
> 
> Recently I came across something weird:
> I have two structs:
> struct s1 { char c1; char c2; short s};
> struct s2 { char c1; short s; char c2};
> 
> A call to sizeof will return 4 for s1 and 6 (!) for s2.
> It seems like the size of the chars in s2 gets increased to 2.
> 
> Does anybody know of a compiler option or some other way to get DJGPP
> to use 1 byte chars regardless of their context?

You should examine the FAQ (v2/faq210b.zip), chapter 22.9, for a
detailed answer to this question.  But in general, any 32-bit compiler
will align variables for maximum efficiency in memory access.  This
sometimes means that struct members get padded to make them "line up" in
memory correctly.

You can use the gcc __attribute__ extension to turn off struct
alignment, or use the less-recommended #pragma pack(1)...#pragma pack()
solution.

-- 
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I  |     mailto:fighteer AT cs DOT com     |
|    *  Proud user of DJGPP!  *    |   http://www.cs.com/fighteer   |
|    ObJoke:  If Bill Gates were a robber, not only would he        |
|     shoot you, but he'd send you a bill for the bullets.          |
---------------------------------------------------------------------

- Raw text -


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