This is the mail archive of the cygwin@cygwin.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

gcc version 2.95.3-5 (cygwin special) Anonymous structures allocated wrong.


While I have learned that cygwin now supports anonymous structures which 
makes some nice things available to me... like derrived structures... I have 
also recently learned that they do not compile correctly...

This program demonstrates the problem

//---------------------------------------------------------------------
//---------------------------------------------------------------------
//---------------------------------------------------------------------

typedef struct thing_tag {
	int x;
	struct {
		int bFlag1:1;
		int bFlag2:1;
		int bFlag3:1;
	};
} THING, *PTHING;

typedef struct other_tag {
	THING;
	int y;
} OTHER, *POTHER;

OTHER o;
PTHING pt = (PTHING)&o;

int MemDump( int len, char *p )
{
	int n;
	for( n = 0; n < len; n++ )
	{
		printf( "%02x ", p[n] );
	}
	printf( "\n" );
}

int main( void )
{
	pt->bFlag1 = 1;
	pt->bFlag2 = 0;
	pt->bFlag3 = 1;
	pt->x = 0x1234;
	printf( "%d %d %d %d\n"
               , pt->bFlag1, pt->bFlag2
               ,   o.bFlag1,   o.bFlag2 );
	MemDump( sizeof(THING), (char*)pt );
	MemDump( sizeof(OTHER), (char*)&o );

	pt->bFlag1 = 0;
	pt->bFlag2 = 0;
	pt->bFlag3 = 0;
	o.x = 0x1234;
	o.bFlag1 = 1;
	o.bFlag2 = 0;
	o.bFlag3 = 1;
	printf( "%d %d %d %d\n"
               , pt->bFlag1, pt->bFlag2
               ,   o.bFlag1,   o.bFlag2 );
	MemDump( sizeof(THING), (char*)pt );
	MemDump( sizeof(OTHER), (char*)&o );

}

//---------------------------------------------------------------------
//---------------------------------------------------------------------
//---------------------------------------------------------------------

The struct { flags }; should be in the same location in both a THING and
and OTHER which includes an anonymous THING as it's first element.  however, 
the first print demonstrates that printing pt->flag1 and o.flag1 do not 
print the same value.  Actually in OTHER the int x; and struct { flags }; 
overlap and all share the same memory... the value printed at the memory 
dump printf 35 12 00 ... the 35 is 34 plus the 1 bit set by setting o.flag1.

Having considered this - I added another element after the flags structure - 
and it is in the correct location in both structures... it's the anonymous 
structure containing flags that is moved to offset 0 in the OTHER 
structure...

typedef struct thing_tag {
        int a, b;
	int x;
	struct {
		int bFlag1:1;
		int bFlag2:1;
		int bFlag3:1;
	};
} THING, *PTHING;

typedef struct other_tag {
	THING;
	int y;
} OTHER, *POTHER;

setting flags in OTHER overwrite THING.a

... Perhaps this will give someone who has worked on this enough 
information...



_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com


--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Bug reporting:         http://cygwin.com/bugs.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]