Related articles |
---|
memory layouts of C++ classes tomtzigt@frc602.intel.com (1994-08-12) |
Re: memory layouts of C++ classes cliffc@rice.edu (1994-08-14) |
memory layouts of C++ classes ssimmons@convex.com (1994-08-14) |
Re: memory layouts of C++ classes jangr@microsoft.com (1994-08-18) |
Newsgroups: | comp.compilers |
From: | ssimmons@convex.com (Steve Simmons) |
Keywords: | C++, code, design, comment |
Organization: | CONVEX News Network, Engineering (cnn.eng), Richardson, Tx USA |
References: | 94-08-087 |
Date: | Sun, 14 Aug 1994 15:48:29 GMT |
> After walking the heap to reverse engineer the memory layout
> of a class in MFC, I figured this forum might provide a better
> answer. How does a C++ compiler build the memory layout of a class
> and a derived class?
First, memory layout is not defined by the language... It is an
implementation issue; however, a common approach is the following...
Given:
Class A{
field a;
}
Class B{
field b;
}
Class C{
field c;
}
No Inheritance:
For class A, the layout would be:
first field: pointer to virtual table A
second field: the field a.
Single Inheritance:
For B derived from A (class B : public A), the layout would be:
first field: pointer to virtual table for B... first
entries in this table are inherited/overriden
virtual functions of A.
second field: the field a.
third field: the field b.
Multiple Inheritance:
For C derived from both A and B (class C : public A, public B), the
layout would be:
first field: pointer to virtual table for C... first
entries in this table are inherited/overriden
virtual functions of A.
second field: the field a.
third field: pointer to virtual for C...first
entries in this table are inherited/overriden
virtual functions of B.
fourth field: the field a in B's class.
fifth field the field b
sixth field: the field c.
Virtual Inheritance:
As you can see in the above example, field A is replicated twice in
Class C. If you want only only one field A, you would need to
do the following:
class A
class B : public virtual A
class C : public virtual A, public B
The layout for C would be the following:
first field: pointer to virtual table for C... first
entries in this table are inherited/overriden
virtual functions of A.
second field: pointer to field a.
third field: the field a. (points to second field)
fourth field: pointer to virtual for C...first
entries in this table are inherited/overriden
virtual functions of B.
fifth field: pointer to field a (should point to second field)
sixth field: field a (never used, cheaper to allocate here as
opposed to doing a malloc).
seventh field the field b
eighth field: the field c.
> Also, what is the difference in memory layout
> of a regular C++ compiler and the memory layouts used by SOM compilers
> to provide binary consistency?
This would be interesting to hear... does someone out there know???
Thank you.
Steve Simmons
[What are SOM compilers? -John]
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.