delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/01/20/16:53:35

From: kagel AT quasar DOT bloomberg DOT com
Date: Mon, 20 Jan 1997 16:23:57 -0500
Message-Id: <9701202123.AA03009@quasar.bloomberg.com >
To: zager AT post DOT comstar DOT ru
Cc: djgpp AT delorie DOT com
In-Reply-To: <32E4505B.7ED0@post.comstar.ru> (message from Dim Zegebart on Mon, 20 Jan 1997 21:12:59 -0800)
Subject: Re: Bug in DJGPP or some my Error?
Reply-To: kagel AT dg1 DOT bloomberg DOT com

   Date: Mon, 20 Jan 1997 21:12:59 -0800
   From: Dim Zegebart <zager AT post DOT comstar DOT ru>

   Try this code. 
   Here i declare some struct with
   members of unsigned char and unsigned int.
   I think what sizeof(unsigned int)+sizeof(unsigned char)=5,
   but I'm wrong! :-(
   DGJPP think another way:

[SNIP]

   #define BFLAG unsigned char //I think sizeof(BFLAG) - 1 byte
   #define HND   unsigned int  //I think sizeof(HND) - 4 byte

   typedef struct
   {
     BFLAG nCode;
     HND   hItem;
   } TItem;

[SNIP]
  TItem pItem;

     printf ("Size of BFLAG : %d\n",sizeof(BFLAG)); // 1
     printf ("Size of HND   : %d\n",sizeof(HND));   // 4 
     printf ("Size of TItem : %d\n",sizeof(TItem)); // 8
   }
[SNIP]

This is correct.  What you missed is that DJGPP must pad the structure between
the field nCode and the field hItem so that hItem will be aligned on a proper
boundary address for performance reasons.  (Print out the addresses of the two
fields to verify this yourself.)  If you reverse the order of the fields in the
structure the padding will still be there, but will move to the end of the
structure, to insure that any array of type TItem will have all of its elements
properly aligned also.

Add the following to verify:

	printf( "Address of nCode: %p\n", &pItem.nCode );
	printf( "Address of hItem: %p\n", &pItem.hItem );
	printf( "Address of pItem: %p\n", &pItem.pItem );
	printf( "Address of pItem[1]: %p\n", ((&pItem.pItem) + 1) );

-- 
Art S. Kagel, kagel AT quasar DOT bloomberg DOT com

A proverb is no proverb to you 'till life has illustrated it.  -- John Keats

- Raw text -


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