Thread: RE: [GD-General] packing fun
Brought to you by:
vexxed72
|
From: Daniel V. <vo...@ep...> - 2003-12-28 12:43:11
|
As a follow up, We were seeing the odd behaviour with variants of gcc 3.1 we used for the Linux64 and Mac port. The 3.3 series of gcc packs the same way as VS.NET and 2.95.3 so I assume it was a regression. I couldn't find anything in the documentation but Microsoft confirmed that VS.NET leaves the padding on base types untouched when allocating space for new members in the derived type. -- Daniel, Epic Games Inc. > -----Original Message----- > From: gam...@li... > [mailto:gam...@li...] On > Behalf Of Daniel Vogel > Sent: Wednesday, December 24, 2003 8:36 PM > To: gam...@li... > Subject: [GD-General] packing fun > > Packing fun. > > // both gcc 3.0 and VS.NET support the pack pragma > #pragma pack(4) > > struct Alice > { > int Integer1; > char Char1; > }; > > struct Bob : Alice > { > char Char2; > }; > > gcc 3.0 > sizeof(Alice)==sizeof(Bob)==8 and funnier enough, > STRUCT_OFFSET(Char2) < > sizeof(Alice) > > VS.NET 2003 > sizeof(Alice)==8, sizeof(Bob)==12 > > Is anyone aware of in-detail documentation on variable > packing for both gcc > and VS.NET? > > Thanks, > > -- Daniel, Epic Games Inc. > > > > ------------------------------------------------------- > This SF.net email is sponsored by: IBM Linux Tutorials. > Become an expert in LINUX or just sharpen your skills. Sign > up for IBM's > Free Linux Tutorials. Learn everything from the bash shell > to sys admin. > Click now! http://ads.osdn.com/?ad_id=1278&alloc_id=3371&op=click > _______________________________________________ > Gamedevlists-general mailing list > Gam...@li... > https://lists.sourceforge.net/lists/listinfo/gamedevlists-general > Archives: > http://sourceforge.net/mailarchive/forum.php?forum_id=557 > |
|
From: Scoubidou944 <Sco...@ho...> - 2003-12-30 18:08:37
|
Don't sure about what your problem is on padding.
#pragma pack(push)
#pragma pack(4)
struct Alice
{
int Integer1;
char Char1;
// HERE COMPILER WILL ADD PADDING BYTES TO 0
char u8Padding1[3]; // == 0
};
struct Bob : Alice
{
char Char2;
// HERE COMPILER WILL ADD PADDING BYTES TO 0
char u8Padding2[3]; // == 0
};
#pragma pack(pop)
So in memory, we will have 2 structure which are both aligned on 4 bytes :
Integer1 Char1 u8Padding1 Char2 u8Padding2
| 4 1 3 | 1 3
|
+-------------------------------+---------------------+
| Alice | Bob
|
If you don't want to loose space in memory (is there any warrior of low
memory using in C++ coding ;p) push & pop the stack to fit to the minimum
size required or reduced size of variables like this :
struct Alice
{
int i24Integer1 : 24; // Will have previous Integer1
which fit in 24 bits instead 32
int i8Integer1 : 8; // Will have previous Char1
};
==> sizeof (Alice) = 4 instead 8 and we keep 4 bytes alignment.
Vincent
|
|
From: Daniel V. <vo...@ep...> - 2003-12-30 18:38:17
|
My problem was that it's not documented what the compiler exactly does wrt
to packing for inheritance with VC++ (my last post contained the
confirmation from Microsoft that it's doing what you would expect with no
nasty surprises) and that the variant of gcc 3.1 we used for Mac and Linux64
versions of UT2003 packed in the strange way I mentioned in a previous post.
This seemed to have been as a regression as 3.3.2 packs like VC++ again.
Our scripting language requires properties to match up exactly with auto-
generated C++ headers so knowing exactly how different compilers pack (and
how to get them all to pack the same way) is vital for us :)
-- Daniel, Epic Games Inc.
> -----Original Message-----
> From: gam...@li...
> [mailto:gam...@li...] On
> Behalf Of Scoubidou944
> Sent: Tuesday, December 30, 2003 1:09 PM
> To: gam...@li...
> Subject: Re: [GD-General] packing fun
>
> Don't sure about what your problem is on padding.
>
> #pragma pack(push)
> #pragma pack(4)
> struct Alice
> {
> int Integer1;
> char Char1;
> // HERE COMPILER WILL ADD PADDING BYTES TO 0
> char u8Padding1[3]; // == 0
> };
>
> struct Bob : Alice
> {
> char Char2;
> // HERE COMPILER WILL ADD PADDING BYTES TO 0
> char u8Padding2[3]; // == 0
> };
> #pragma pack(pop)
>
> So in memory, we will have 2 structure which are both aligned
> on 4 bytes :
> Integer1 Char1 u8Padding1 Char2 u8Padding2
> | 4 1 3 | 1 3
> |
> +-------------------------------+---------------------+
> | Alice | Bob
> |
>
> If you don't want to loose space in memory (is there any
> warrior of low
> memory using in C++ coding ;p) push & pop the stack to fit to
> the minimum
> size required or reduced size of variables like this :
>
> struct Alice
> {
> int i24Integer1 : 24; // Will have
> previous Integer1
> which fit in 24 bits instead 32
> int i8Integer1 : 8; // Will have
> previous Char1
> };
> ==> sizeof (Alice) = 4 instead 8 and we keep 4 bytes alignment.
>
> Vincent
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: IBM Linux Tutorials.
> Become an expert in LINUX or just sharpen your skills. Sign
> up for IBM's
> Free Linux Tutorials. Learn everything from the bash shell
> to sys admin.
> Click now! http://ads.osdn.com/?ad_id=1278&alloc_id=3371&op=click
> _______________________________________________
> Gamedevlists-general mailing list
> Gam...@li...
> https://lists.sourceforge.net/lists/listinfo/gamedevlists-general
> Archives:
> http://sourceforge.net/mailarchive/forum.php?forum_id=557
>
|