 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guilherme Pinto Guest
|
Posted: Thu Oct 16, 2003 5:56 pm Post subject: Virtual Function Table and Class attributes Memory Managemen |
|
|
Hello,
I have three main doubts.
1)I saw that non-static attribues of a class are stored in contiguous
positions in memory.My doubt is if at some point of compilation the
assumption below is made. If it is, when ?
To each access to the class variables the declaration of the class
is read then the position in the memory is obtained, for example, the
second atribute
will have the position indicated by the position of the object(Address
of the first attribute) plus the size of the first attribute.
I also wonder that this process must take account of inheritance and
of the virtual function table.
2) I also saw that the address of the virtual function table if it
exists is the same address od the object, that is, it is allocated at
first. Is it possible to get the address of the functions of the
virtual function table. I want to get this address to assign to a
function pointer of same type.
There is not a special purpose to do this, I only want to make some
tests.
3) In which book these kind of information are available ?
Thanks,
Guilherme Pinto
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michael Tiomkin Guest
|
Posted: Fri Oct 17, 2003 7:59 am Post subject: Re: Virtual Function Table and Class attributes Memory Manag |
|
|
[email]guilhermepinto (AT) urbi (DOT) com.br[/email] (Guilherme Pinto) wrote in message news:<347603fb.0310160707.183b1b94 (AT) posting (DOT) google.com>...
| Quote: | Hello,
I have three main doubts.
1)I saw that non-static attribues of a class are stored in contiguous
positions in memory.My doubt is if at some point of compilation the
assumption below is made. If it is, when ?
To each access to the class variables the declaration of the class
is read then the position in the memory is obtained, for example, the
second atribute
will have the position indicated by the position of the object(Address
of the first attribute) plus the size of the first attribute.
I also wonder that this process must take account of inheritance and
of the virtual function table.
|
Well, in most cases the answer is positive. First of all, as an
implementation issue, it's not defined by the standard. However,
the most simple (and efficient) solution is to place the attributes
of the top ancestor class first, then the attributes of its child class,
and at the end the attributes of the object class. This simplifies
the runtime access to the attributes of an object when you deal
with a ptr/reference to it, i.e. when the compiler doesn't know
its exact type.
The case of multiple inheritance is more complicated,
especially if the parents have common ancestors.
There are only two aditional considerations. First, the alignment
of every attribute is important, and therefore there might be gaps
in the object structure. Second, a ptr to the virtual functions table
is usually put as the first attribute of the first class that has virtual
functions. In case that the top ancestor class doesn't have virtual
methods, this pointer will be placed in the middle or at the end
of an object data.
| Quote: | 2) I also saw that the address of the virtual function table if it
exists is the same address od the object, that is, it is allocated at
first. Is it possible to get the address of the functions of the
virtual function table. I want to get this address to assign to a
function pointer of same type.
There is not a special purpose to do this, I only want to make some
tests.
|
As was said above, even in the most simple implementation
the vptr is not always the first item of the object data.
As usual, in order to access implementation data, you need more
or less dirty tricks. In this case, assume that the class B defines
virtual methods, all the ancestors of B do not define virtual methods,
and the parent of B is A. Then in this implementation the vptr
of an object 'o' of class B or any its descendants might be
*(void **)((void *)&o + sizeof(A)) // add alignment for a ptr?
When A doesn't exist, i.e. when B is the top ancestor, use 0
instead of 'sizeof(A)'.
BTW, you can use pointers to methods instead of using vptr.
Notice that even if you got the method address, you cannot call
the method as a function. The calling convention for calling
a method is different: e.g. you have to set 'this' to an object.
Of course, you can read a disassembly from a method call
and then use inlined assembly to do the call!-)
BTW, I'm not sure that this information will be very helpful
for you, unless you intend to write a C++ compiler.
| Quote: | 3) In which book these kind of information are available ?
|
I recall hearing an explanation on attribute/method placement
for multiple inheritance at least 10 years ago, but I forgot both
the placement algorithm and the reference where it was described.
As usual, the source of g++ can be very helpful.
Enjoy!
Michael
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Fri Oct 17, 2003 3:41 pm Post subject: Re: Virtual Function Table and Class attributes Memory Manag |
|
|
[email]guilhermepinto (AT) urbi (DOT) com.br[/email] (Guilherme Pinto) wrote in message
news:<347603fb.0310160707.183b1b94 (AT) posting (DOT) google.com>...
| Quote: | I have three main doubts.
1)I saw that non-static attribues of a class are stored in contiguous
positions in memory. My doubt is if at some point of compilation the
assumption below is made. If it is, when ?
To each access to the class variables the declaration of the class
is read then the position in the memory is obtained, for example, the
second atribute
|
For each access, the compiler determines the address of the object, then
adds the offset of the attribute, which it addes to the address of the
object to access the attribute.
| Quote: | will have the position indicated by the position of the object(Address
of the first attribute) plus the size of the first attribute.
|
Not necessarily. The compiler has a good deal of freedom.
| Quote: | I also wonder that this process must take account of inheritance and
of the virtual function table.
|
Obviously. If the compiler places them in front of the attribute.
| Quote: | 2) I also saw that the address of the virtual function table if it
exists is the same address od the object, that is, it is allocated at
first.
|
That's one possible implementation. It's not the only one.
| Quote: | Is it possible to get the address of the functions of the virtual
function table.
|
For a given implementation, using undefined behavior, perhaps. In
general, no.
| Quote: | I want to get this address to assign to a function pointer of same
type. There is not a special purpose to do this, I only want to make
some tests.
|
It is possible for a given specific implementation. Usually, at least;
I suppose that some implementations may make it impossible. But you
have to know the details of the internal workings of your compiler;
things which generally aren't documented, and that can change from one
version to the next.
| Quote: | 3) In which book these kind of information are available ?
|
I think that Lipmann had something along these lines, although it really
only considered the internals of CFront. And Coplien actually shows
code which does this -- again, for CFront.
In the case of g++, the sources are available, so you should be able to
find out the information too.
For any other compiler, you'll probably just have to guess.
--
James Kanze GABI Software mailto:kanze (AT) gabi-soft (DOT) fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientierter Datenverarbeitung
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Matthias Hofmann Guest
|
Posted: Sat Oct 18, 2003 9:26 am Post subject: Re: Virtual Function Table and Class attributes Memory Manag |
|
|
Guilherme Pinto <guilhermepinto (AT) urbi (DOT) com.br> schrieb in im Newsbeitrag:
[email]347603fb.0310160707.183b1b94 (AT) posting (DOT) google.com[/email]...
| Quote: |
[snip]
3) In which book these kind of information are available ?
|
Here's a good article on that topic by Jan Gray. It tells you quite a lot
about memory layout of objects and virtual functions, mostly focused on
VC++, but it also hints at other implementations:
http://www.cs.wustl.edu/~inder/oop/Gray-article.html
Regards,
Matthias Hofmann
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Sat Oct 18, 2003 2:30 pm Post subject: Re: Virtual Function Table and Class attributes Memory Manag |
|
|
In article <347603fb.0310160707.183b1b94 (AT) posting (DOT) google.com>,
Guilherme Pinto wrote:
| Quote: | Hello,
I have three main doubts.
1)I saw that non-static attribues of a class are stored in contiguous
positions in memory.
|
(I assume that by attributes you mean member variables.) The standard
only guarantees that a series of non-static member variables declared
without any access specifier between them will be laid-out in memory
in the order they are declared.
| Quote: | My doubt is if at some point of compilation the
assumption below is made. If it is, when ?
To each access to the class variables the declaration of the class
is read then the position in the memory is obtained, for example, the
second atribute
will have the position indicated by the position of the object(Address
of the first attribute) plus the size of the first attribute.
|
No, there may be padding to satisfy the alignment requirements of
the target processor. For example, in this structure:
struct A { char c; int i; };
i will typically be offset by 4 bytes from the beginning of the
class, rather than by 1 byte as you suggest.
| Quote: | I also wonder that this process must take account of inheritance and
of the virtual function table.
|
Yes, but these are implementation details. The standard specifies
very little about the layout of a class with base classes and/or
virtual functions.
| Quote: | 2) I also saw that the address of the virtual function table if it
exists is the same address od the object, that is, it is allocated at
first. Is it possible to get the address of the functions of the
virtual function table. I want to get this address to assign to a
function pointer of same type.
|
No, the existence and structure of virtual function tables are
implementation details and there is no portable way to access them.
Furthermore they are often be write-protected, so that even if you
can read them, an attempt to modify them will crash your program.
| Quote: | There is not a special purpose to do this, I only want to make some
tests.
3) In which book these kind of information are available ?
|
"Inside the C++ Object Model" by Stanley B. Lippman explains how
CFront, the original C++ compiler, implemented classes. Other
implementations work somewhat differently. See
<http://www.amazon.com/exec/obidos/tg/detail/-/0201834545> for full
details of the book.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
stelios xanthakis Guest
|
Posted: Sat Oct 18, 2003 9:46 pm Post subject: Re: Virtual Function Table and Class attributes Memory Manag |
|
|
[email]guilhermepinto (AT) urbi (DOT) com.br[/email] (Guilherme Pinto) wrote in message news:<347603fb.0310160707.183b1b94 (AT) posting (DOT) google.com>...
| Quote: | Hello,
I have three main doubts.
|
See
http://students.ceid.upatras.gr/~sxanth/lwc/Examples.html
for a way these can be done.
Also search google for "C++ ABI". there are some good examples
in codesourcery.
stelios
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|