This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
c++/7201: Multiple inheritance from classes without data members wastes memory
- From: markus at noga dot de
- To: gcc-gnats at gcc dot gnu dot org
- Date: 3 Jul 2002 18:04:29 -0000
- Subject: c++/7201: Multiple inheritance from classes without data members wastes memory
- Reply-to: markus at noga dot de
>Number: 7201
>Category: c++
>Synopsis: Multiple inheritance from classes without data members wastes memory
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Wed Jul 03 11:06:00 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator: Markus L. Noga
>Release: 2.96 20000731 (Red Hat Linux 7.3 2.96-110), 2.95.3-5 (cygwin special)
>Organization:
>Environment:
2.96 20000731 (Red Hat Linux 7.3 2.96-110) and
2.95.3-5 (Windows XP Professional, cygwin special)
>Description:
Classes without data members occupy at least one byte,
even if they do not require a vtable pointer:
class X1 {};
-> sizeof(X1)==1
When inheriting from classes without data member, the mysterious bytes add up:
class X2 {};
class X3 : public X1, public X2 {};
-> sizeof(X3)==2
They even persist when finally inheriting from a class
with data members:
class X4 { int i; };
class X5 : public X3, public X4 {};
-> sizeof(X5)==8 (due to alignment)
Forcing packed structures does not change this.
>How-To-Repeat:
#include <iostream>
class X1 {};
class X2 {};
class X3 : public X1, public X2 {};
class X4 { int i; };
class X5 : public X3, public X4 {};
int main(int argc, char **argv) {
cout << sizeof(X1) << endl;
cout << sizeof(X2) << endl;
cout << sizeof(X3) << endl;
cout << sizeof(X4) << endl;
cout << sizeof(X5) << endl;
return 0;
}
>Fix:
>Release-Note:
>Audit-Trail:
>Unformatted: