Mail Archives: djgpp/1997/01/20/16:53:35
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 -