This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
possible structure layout bug
- From: Stephen Kennedy <Stephen dot Kennedy at havok dot com>
- To: gcc at gcc dot gnu dot org
- Date: Wed, 09 Jun 2004 18:22:24 +0100
- Subject: possible structure layout bug
- Organization: Havok
- Reply-to: Stephen dot Kennedy at havok dot com
Normally, compilers place members from derived classes starting at
sizeof(base), but gcc breaks this rule if the class contains a simd type
and places the derived member in the padding of the base class.
Reproduced with gcc-3.3 and gcc-3.4 on debian linux x86.
See the attached case
%g++-3.4 layoutbug.cpp && a.out
__m128 size 16, align 16
Quad size 16, align 16
Base1 32 Derived1 48 Offset 32 <- offset follows sizeof(base)
Base2 32 Derived2 32 Offset 20 <- offset of derived member is inside
Base3 64 Derived3 80 Offset 64 <- sizeof(base class)
Has anybody any comments? I know layouts vary from compiler to compiler,
but this seems to be inconsistent with itself.
Stephen.
--
Stephen Kennedy stephen.kennedy@havok.com http://www.havok.com/
Game Developer Frontline Award Winner 2002 & 2003
Computer Graphics World 2003 Innovation Award Winner
#include <stdio.h>
typedef int __m128 __attribute__ ((__mode__(__V4SF__)));
struct Quad
{
float x __attribute__((aligned(16)));
float y;
float z;
float w;
};
struct Base1
{
Quad q;
int i;
};
struct Derived1 : public Base1
{
int i2;
};
struct Base2
{
__m128 m;
int i;
};
struct Derived2 : public Base2
{
int i2;
};
struct Base3
{
Quad q0;
__m128 m;
Quad q1;
int i;
};
struct Derived3 : public Base3
{
int i2;
};
#define offset(C,M) (int((&((C*)(16))->M))-16)
int main()
{
printf("__m128 size %i, align %i\n", sizeof(__m128), __alignof__(__m128));
printf("Quad size %i, align %i\n", sizeof(Quad), __alignof__(Quad));
printf("Base1 %i Derived1 %i Offset %i\n", sizeof(Base1), sizeof(Derived1), offset(Derived1, i2) );
printf("Base2 %i Derived2 %i Offset %i\n", sizeof(Base2), sizeof(Derived2), offset(Derived2, i2) );
printf("Base3 %i Derived3 %i Offset %i\n", sizeof(Base3), sizeof(Derived3), offset(Derived3, i2) );
return 0;
}