delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2003/07/04/22:31:38

From: Andrew <andnews AT ihug DOT com DOT oz DOT au>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: data alignment issue
Date: Sat, 05 Jul 2003 12:27:14 +1000
Organization: ECLiPSE
Lines: 33
Message-ID: <4idcgvk4irbbno3e54826727aqa750en85@4ax.com>
References: <00A224F4 DOT 86EC817C DOT 6 AT st DOT fmph DOT uniba DOT sk>
NNTP-Posting-Host: p250-tnt6.syd.ihug.com.au
Mime-Version: 1.0
X-Trace: lust.ihug.co.nz 1057372036 19075 203.173.146.250 (5 Jul 2003 02:27:16 GMT)
X-Complaints-To: abuse AT ihug DOT co DOT nz
NNTP-Posting-Date: Sat, 5 Jul 2003 02:27:16 +0000 (UTC)
X-Newsreader: Forte Agent 1.93/32.576 English (American)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

On Thu, 3 Jul 2003 14:04:47 +0200, Viktor DOT Stujber AT st DOT fmph DOT uniba DOT sk
wrote:

>I've noticed that djgpp aligns all members of a structure to 4 bytes.
>---------------------------------------------------------------------
>| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
>---------------------------------------------------- 
>| a   _   _   _   b   b   b   b   c    c    _    _ |
>----------------------------------------------------
>
>this is the layout of this structure in memory:
>struct {
> BYTE a;
> DWORD b;
> WORD c;
>}
>
>because of the 4byte alignment, sizeof() returns 12 instead of 7.

This is normal with all modern 32 bit compilers due to optimizations
in the compiler becuase the CPU loads data at a 4 byte boundary allot
faster that at non 4 byte boundaries.

Have a look in the faq\DJGPPFAQ.TXT file for pack. There are a two
different ways of coding it so that the struct is packed, but be aware
that you will pay a speed penalty for packing the structure.

See setion 18.5 & 22.11 & 22.12 for examples of the two options you
can use. The #pragma optino is portable to some other compilers such
as Borland.

Andrew

- Raw text -


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