This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Darwin gcc 3.3 sizeof of C++ structures with long long ints is strange
- From: "Kevin B. Hendricks" <kevin dot hendricks at sympatico dot ca>
- To: gcc at gcc dot gnu dot org
- Date: Fri, 7 Feb 2003 11:17:50 -0500
- Subject: Darwin gcc 3.3 sizeof of C++ structures with long long ints is strange
Hi,
I am exploring the use of gcc 3.3 (based on CVS from yesterday) to build
OpenOffice.org under MacOSX/Darwin (we have had troubles with Apple's gcc
3.1 that don't seem to happen under gcc 3.3)?
Unfortunately, the following structure alignment program shows the size of
structure C5 is 4 bytes larger than expected.
If you look at the code the key issue is that C5 uses a long long int type.
If I replace the long long int with a double (both are 8 bytes in size)
the expected size is achieved.
Will someone familar with C++ structure alignment and Darwin please take a
look at this testcase for me?
Is there something funny happening with long long ints here?
Thanks,
Kevin
My gcc 3.3 build is installed in /usr/local/
This is on MacOSX 10.2.3
setenv PATH /usr/local/bin:${PATH}
setenv DYLD_LIBRARY_PATH /usr/local/lib
[khendricksmac:~] kbhend% /usr/local/bin/g++ -DMAX4 -DADJUST_ALIGN -O2 -o
datatest datatest.cxx
[khendricksmac:~] kbhend% ./datatest
> sizeof(AlignSize_Impl) = 12; __alignof__ (AlignSize_Impl) = 4
> sizeof(M) = 8; __alignof__ (M) = 4
> sizeof(N) = 12; __alignof__ (N) = 4
> sizeof(N2) = 12; __alignof__ (N2) = 4
> sizeof(O) = 16; __alignof__ (O) = 4
> sizeof(D) = 8; __alignof__ (D) = 4
> sizeof(C1) = 2; __alignof__ (C1) = 2
> sizeof(C2) = 8; __alignof__ (C2) = 4
> sizeof(C3) = 20; __alignof__ (C3) = 4
> sizeof(C4) = 32; __alignof__ (C4) = 4
> sizeof(C5) = 48; __alignof__ (C5) = 8
### sizeof(C5) = 48 instead of expected 44!!!
> sizeof(C6) = 64; __alignof__ (C6) = 8
### sizeof(C6) = 64 instead of expected 52!!!
### OFFSET_OF(C6, c6) = 8 instead of expected 4!!!
### OFFSET_OF(C6, b6) = 56 instead of expected 48!!!
> sizeof(O2) = 24; __alignof__ (O2) = 4
> sizeof(Char3) = 3; __alignof__ (Char3) = 1
> sizeof(P) = 20; __alignof__ (P) = 4
Here is the testcase the issue seems to happen with long long types only
(see the C5 structure defintion)
cat datatest.cxx
#include <stdlib.h>
#include <stdio.h>
typedef struct _Any
{
void * pType;
void * pData;
void * pReserved;
} uno_Any;
typedef _Any Any;
// Patching the gcc 3 incompatible alignment change for linux
// This pragma macro is appended to every first member of a struct,
// iff the struct inherits from a base struct and the first member
// of this structure is not a double or [unsigned] long long.
#ifdef ADJUST_ALIGN
#define CPPU_GCC3_ALIGN( base_struct ) __attribute__ ((aligned (__alignof__
(base_struct))))
#else
#define CPPU_GCC3_ALIGN( base_struct )
#endif
typedef enum {
My_BYTE,
MY_CHAR,
MY_INT,
MY_LONG,
MY_LONGLONG,
MY_FLOAT,
MY_DOUBLE
} TypeClass;
#ifdef MAX4
#define MAX_ALIGNMENT_4
#endif
#define OFFSET_OF( s, m ) ((size_t)((char *)&((s *)16)->m -16))
#define BINTEST_VERIFY( c ) \
if (! (c)) { fprintf( stderr, "### binary compatibility test failed: "
#c " [line %d]!!!\n", __LINE__ ); /* abort();*/ }
#define BINTEST_VERIFYOFFSET( s, m, n ) \
if (OFFSET_OF(s, m) != n) { fprintf( stderr, "### OFFSET_OF(" #s ", "
#m ") = %d instead of expected %d!!!\n", OFFSET_OF(s, m), n ); /*
abort();*/ }
#define BINTEST_VERIFYSIZE( s, n ) \
fprintf( stderr, "> sizeof(" #s ") = %d; __alignof__ (" #s ") = %d\n",
sizeof(s), __alignof__ (s) ); \
if (sizeof(s) != n) { fprintf( stderr, "### sizeof(" #s ") = %d instead
of expected %d!!!\n", sizeof(s), n ); /* abort();*/ }
struct C1
{
signed short n1;
};
struct C2 : public C1
{
signed long n2 CPPU_GCC3_ALIGN( C1 );
};
struct C3 : public C2
{
double d3;
signed long n3;
};
struct C4 : public C3
{
signed long n4 CPPU_GCC3_ALIGN( C3 );
double d4;
};
struct C5 : public C4
{
signed long long n5;
unsigned char b5;
};
struct C6 : public C1
{
C5 c6 CPPU_GCC3_ALIGN( C1 );
unsigned char b6;
};
struct D
{
signed short d;
signed long e;
};
struct E
{
unsigned char a;
unsigned char b;
unsigned char c;
signed short d;
signed long e;
};
struct M
{
signed long n;
signed short o;
};
struct N : public M
{
signed short p CPPU_GCC3_ALIGN( M );
};
struct N2
{
M m;
signed short p;
};
struct O : public M
{
double p;
};
struct O2 : public O
{
double p2;
};
struct P : public N
{
double p2;
};
struct empty
{
};
struct second : public empty
{
int a;
};
struct AlignSize_Impl
{
signed short nInt16;
double dDouble;
};
struct Char1
{
char c1;
};
struct Char2 : public Char1
{
char c2 CPPU_GCC3_ALIGN( Char1 );
};
struct Char3 : public Char2
{
char c3 CPPU_GCC3_ALIGN( Char2 );
};
struct Char4
{
Char3 chars;
char c;
};
class BinaryCompatible_Impl
{
public:
BinaryCompatible_Impl();
};
BinaryCompatible_Impl::BinaryCompatible_Impl()
{
#ifdef MAX_ALIGNMENT_4
// max alignment is 4
BINTEST_VERIFYOFFSET( AlignSize_Impl, dDouble, 4 );
BINTEST_VERIFYSIZE( AlignSize_Impl, 12 );
#else
// max alignment is 8
BINTEST_VERIFYOFFSET( AlignSize_Impl, dDouble, 8 );
BINTEST_VERIFYSIZE( AlignSize_Impl, 16 );
#endif
// enum
BINTEST_VERIFY( sizeof( TypeClass ) == sizeof( signed long ) );
// any
BINTEST_VERIFY( sizeof(void *) >= sizeof(signed long) );
BINTEST_VERIFY( sizeof( Any ) == sizeof( _Any ) );
BINTEST_VERIFY( sizeof( Any ) == sizeof( void * ) * 3 );
BINTEST_VERIFYOFFSET( Any, pType, 0 );
BINTEST_VERIFYOFFSET( Any, pData, 4 );
BINTEST_VERIFYOFFSET( Any, pReserved, 8 );
// struct
BINTEST_VERIFYSIZE( M, 8 );
BINTEST_VERIFYOFFSET( M, o, 4 );
BINTEST_VERIFYSIZE( N, 12 );
BINTEST_VERIFYOFFSET( N, p, 8 );
BINTEST_VERIFYSIZE( N2, 12 );
BINTEST_VERIFYOFFSET( N2, p, 8 );
BINTEST_VERIFYSIZE( O, 16 );
BINTEST_VERIFYSIZE( D, 8 );
BINTEST_VERIFYOFFSET( D, e, 4 );
BINTEST_VERIFYOFFSET( E, d, 4 );
BINTEST_VERIFYOFFSET( E, e, 8 );
BINTEST_VERIFYSIZE( C1, 2 );
BINTEST_VERIFYSIZE( C2, 8 );
BINTEST_VERIFYOFFSET( C2, n2, 4 );
#ifdef MAX_ALIGNMENT_4
BINTEST_VERIFYSIZE( C3, 20 );
BINTEST_VERIFYOFFSET( C3, d3, 8 );
BINTEST_VERIFYOFFSET( C3, n3, 16 );
BINTEST_VERIFYSIZE( C4, 32 );
BINTEST_VERIFYOFFSET( C4, n4, 20 );
BINTEST_VERIFYOFFSET( C4, d4, 24 );
BINTEST_VERIFYSIZE( C5, 44 );
BINTEST_VERIFYOFFSET( C5, n5, 32 );
BINTEST_VERIFYOFFSET( C5, b5, 40 );
BINTEST_VERIFYSIZE( C6, 52 );
BINTEST_VERIFYOFFSET( C6, c6, 4 );
BINTEST_VERIFYOFFSET( C6, b6, 48 );
#else
BINTEST_VERIFYSIZE( C3, 24 );
BINTEST_VERIFYOFFSET( C3, d3, 8 );
BINTEST_VERIFYOFFSET( C3, n3, 16 );
BINTEST_VERIFYSIZE( C4, 40 );
BINTEST_VERIFYOFFSET( C4, n4, 24 );
BINTEST_VERIFYOFFSET( C4, d4, 32 );
BINTEST_VERIFYSIZE( C5, 56 );
BINTEST_VERIFYOFFSET( C5, n5, 40 );
BINTEST_VERIFYOFFSET( C5, b5, 48 );
BINTEST_VERIFYSIZE( C6, 72 );
BINTEST_VERIFYOFFSET( C6, c6, 8 );
BINTEST_VERIFYOFFSET( C6, b6, 64 );
#endif
BINTEST_VERIFYSIZE( O2, 24 );
BINTEST_VERIFYOFFSET( O2, p2, 16 );
BINTEST_VERIFYSIZE( Char3, 3 );
BINTEST_VERIFYOFFSET( Char4, c, 3 );
#ifdef MAX_ALIGNMENT_4
// max alignment is 4
BINTEST_VERIFYSIZE( P, 20 );
#else
// alignment of P is 8, because of P[] ...
BINTEST_VERIFYSIZE( P, 24 );
BINTEST_VERIFYSIZE( second, sizeof( int ) );
#endif
}
static BinaryCompatible_Impl aTest;
int main ()
{
exit(0);
}