由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - How to check the virtual function table size?
相关主题
问题:vptr/vtable for virtual function & vptr/vtable for师傅们都出来看看吧,我也问个C++返回值问题。
C++小插曲C++虚函数动态绑定的含义?
A C++ compiler related interview questiontypeid produces a reference to a static type_info
c++里的函数可不可以是virtual+staticC++ Q20: construction and inheritance
C++ 请教: about the memory layout of the class inheritance问个虚函数的作用
为什么derived object没有vptr?问一个C++问题:default parameter and overriding/inheritanc (转载)
我有个很傻的问题,关于function call via pointerC++ Q96: function inheritance (转载)
some c++ question.Two questions on virtual destructor
相关话题的讨论汇总
话题: virtual话题: vtable话题: function话题: vptr话题: size
进入Programming版参与讨论
1 (共1页)
z***e
发帖数: 5393
1
Is there any way to get vptr or check the size of virtual function table?
thx.
c****e
发帖数: 1453
2
Statically, I think you can count the number of virtual functions and times
4.
Here is my guess:
suppose we want to know the vtable size of class B.
build D,
class D : public B
{
virtual void test(){}
};
now, in the memory:
D:
+0: pointer to vtable
vtabble:
... v functions in class B
test()
D * pd= new D;
void (*p)()=pd->test;
char * vtable_base=(char *)(*(int *)pd);
vsize=(char *)p - vtable_base;
t****t
发帖数: 6806
3
看上去很不错,写成template就更牛了

times

【在 c****e 的大作中提到】
: Statically, I think you can count the number of virtual functions and times
: 4.
: Here is my guess:
: suppose we want to know the vtable size of class B.
: build D,
: class D : public B
: {
: virtual void test(){}
: };
: now, in the memory:

d*******d
发帖数: 2050
4

times
~~~~~~~
有一点不明白的,请问这里是为什么。

【在 c****e 的大作中提到】
: Statically, I think you can count the number of virtual functions and times
: 4.
: Here is my guess:
: suppose we want to know the vtable size of class B.
: build D,
: class D : public B
: {
: virtual void test(){}
: };
: now, in the memory:

c*****g
发帖数: 119
5
1. the layout of the object and vtable in memory depends on compiler
implementation
2. you cannot get # of v functions at runtime
3. times by 4 depends on platforms
4. how about multiple inheritence?
.....

times

【在 c****e 的大作中提到】
: Statically, I think you can count the number of virtual functions and times
: 4.
: Here is my guess:
: suppose we want to know the vtable size of class B.
: build D,
: class D : public B
: {
: virtual void test(){}
: };
: now, in the memory:

t****t
发帖数: 6806
6
this is of course implementation defined... the standard doesn't even say
there exists a vtable or vptr.
but,
1. layout of the object is implementation defined, but *usually* vptr is the
first entry.
2. ???
3. you can use sizeof(any_pointer_to_function) instead.
4. this is indeed a big problem.

【在 c*****g 的大作中提到】
: 1. the layout of the object and vtable in memory depends on compiler
: implementation
: 2. you cannot get # of v functions at runtime
: 3. times by 4 depends on platforms
: 4. how about multiple inheritence?
: .....
:
: times

c********x
发帖数: 84
7

http://en.wikipedia.org/wiki/Virtual_table

【在 z***e 的大作中提到】
: Is there any way to get vptr or check the size of virtual function table?
: thx.

c*****g
发帖数: 119
8
2.是说:程序运行的时候没法知道到底一个object有几个virtual functions。如果
class里virtual function改变了,其他的code相应地都得变。

the

【在 t****t 的大作中提到】
: this is of course implementation defined... the standard doesn't even say
: there exists a vtable or vptr.
: but,
: 1. layout of the object is implementation defined, but *usually* vptr is the
: first entry.
: 2. ???
: 3. you can use sizeof(any_pointer_to_function) instead.
: 4. this is indeed a big problem.

c*****g
发帖数: 119
9
原来有typo,cannot中not漏了。

【在 c*****g 的大作中提到】
: 2.是说:程序运行的时候没法知道到底一个object有几个virtual functions。如果
: class里virtual function改变了,其他的code相应地都得变。
:
: the

p***o
发帖数: 1252
10
Well, it's doable if you can change the code and ask others to follow you,
e.g. get a virtual function to allow query such information like COM,
or have a compiler giving such information like Java ...
Otherwise, it's too hard. Do we have any reflection library for C++ now?

【在 c*****g 的大作中提到】
: 2.是说:程序运行的时候没法知道到底一个object有几个virtual functions。如果
: class里virtual function改变了,其他的code相应地都得变。
:
: the

W*********g
发帖数: 409
11
不可行。首先vptr在哪不清楚,微软是放在前面,但是更直接的方法是在后面,如果是
这样,对于derived class来说vptr就在中间了,这还没有考虑multiple inherit(多
个vtbl) 和virtual inherit(virtual base class有自己的vtbl)。
其次vtbl里还有其他东西,比如typeid。有些实现会把this pointer adjustment
offsets放在vtbl里。
1 (共1页)
进入Programming版参与讨论
相关主题
Two questions on virtual destructorC++ 请教: about the memory layout of the class inheritance
C++ cast 小结为什么derived object没有vptr?
ask a C++ inheritance question我有个很傻的问题,关于function call via pointer
A C++ inheritance question!some c++ question.
问题:vptr/vtable for virtual function & vptr/vtable for师傅们都出来看看吧,我也问个C++返回值问题。
C++小插曲C++虚函数动态绑定的含义?
A C++ compiler related interview questiontypeid produces a reference to a static type_info
c++里的函数可不可以是virtual+staticC++ Q20: construction and inheritance
相关话题的讨论汇总
话题: virtual话题: vtable话题: function话题: vptr话题: size