c++/8956: Class layout: exessive padding for simple inheritance case

johnb@stl.sarov.ru johnb@stl.sarov.ru
Mon Dec 16 00:06:00 GMT 2002


>Number:         8956
>Category:       c++
>Synopsis:       Class layout: exessive padding for simple inheritance case
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Mon Dec 16 00:06:00 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     Eugeny Belov
>Release:        g++ (GCC) 3.2 20020903 (Red Hat Linux 8.0 3.2-7), g++ (GCC) 3.2 on UnitedLinux 1.0 (ia64)
>Organization:
>Environment:
Red Hat Linux 8.0 on IA32
United Linux 1.0 on IA64
>Description:
  I found only the similar report #8684 in the bugs database, but not the same. I can reproduce the failure on both IA32 and IA64 plattforms with g++ 3.2.
  Here is the testcase:

#include <iostream>

struct S {
  int i;
  short s;
};

struct T : public S {
  short t;
};

struct U {
  int i;
  short s;
  short t;
};

int main (void)
{
  if (sizeof (T) == sizeof (U))
     std::cout << "PASSED\n";
  else
     std::cout << "FAILED  " << sizeof(T) << "!=" << sizeof (U) << std::endl;
  
  return 0;
}

According to the C++ ABI (http://www.codesourcery.com/cxx-abi/abi.html) it is expected that layout of T should be the same as layout of U and have the same size.
  What is happening when we start to laying out the T  - we have to put at the beginning the base class S, then we should place the T`s short element and finish the layout.  But the trick is that T`s short element should be placed at offset dsize(S) (sizeof(S) without tail padding - padding  is presented in S after short type element). It seems that g++ put the T`s short member at offset sizeof(S) and it is wrong.
>How-To-Repeat:
Compile testcase with g++ v3.2 and run, You`ll see the FAILED message.
>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: text/plain; name="tfields4.cpp"
Content-Disposition: inline; filename="tfields4.cpp"

#include <iostream>

struct S {
  int i;
  short s;
};

struct T : public S {
  short t;
};

struct U {
  int i;
  short s;
  short t;
};

int main (void)
{
  if (sizeof (T) == sizeof (U))
     std::cout << "PASSED\n";
  else
     std::cout << "FAILED  " << sizeof(T) << "!=" << sizeof (U) << std::endl;
  
  return 0;
}



More information about the Gcc-prs mailing list