Redian新闻
>
How to check the virtual function table size?
avatar
How to check the virtual function table size?# Programming - 葵花宝典
z*e
1
Is there any way to get vptr or check the size of virtual function table?
thx.
avatar
c*e
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;
avatar
t*t
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:

avatar
d*d
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:

avatar
c*g
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:

avatar
t*t
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

avatar
c*g
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.

avatar
c*g
9
原来有typo,cannot中not漏了。

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

avatar
p*o
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

avatar
W*g
11
不可行。首先vptr在哪不清楚,微软是放在前面,但是更直接的方法是在后面,如果是
这样,对于derived class来说vptr就在中间了,这还没有考虑multiple inherit(多
个vtbl) 和virtual inherit(virtual base class有自己的vtbl)。
其次vtbl里还有其他东西,比如typeid。有些实现会把this pointer adjustment
offsets放在vtbl里。
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。