简体   繁体   English

如何确定VMT指针的数量

[英]How to determine count of VMT pointers

using Visual C++ compiler every class object have VMT ( first pointer in object) which is the pointer to array of pointers to every method in class. 使用Visual C ++编译器,每个类对象都有VMT (对象中的第一个指针), VMT是指向类中每个方法的指针数组的指针。 Using code like this: 使用如下代码:

TestObject * object = new TestObject();
void** VMT = (void**)object;
int TestCount = 100;

for( int i = 0; i < TestCount; i ++ )
{
     printf("%d function: %p\r\n", (i+1), VMT[i] );
}

This code enumerates first 100 functions in class, but how can i determine how many those pointers class have considering that i don't have class definition? 该代码枚举了类中的前100个函数,但是考虑到我没有类定义,我如何确定那些指针类有多少个呢? How to find out it dynamically? 如何动态找到它?

Thanks! 谢谢!

Just don't do this. 只是不要这样做。

Firstly, a class that has no virtual members has no V-table, hence your attempt to fetch it will at best give an invalid pointer and at worst crash. 首先,没有虚拟成员的类没有V表,因此,您尝试获取它的过程充其量将给出无效的指针,并在最坏的情况下崩溃。

Secondly, even if you do get it, you'll only find pointers to virtual member functions in it, normal functions aren't placed in the V-table. 其次,即使您确实获得了它,也只会在其中找到指向虚拟成员函数的指针,而普通函数不会放在V表中。

Thirdly, while I do note that you're using MSVC, so portability is less of an issue, this is hideously non-portable. 第三,虽然我确实注意到您正在使用MSVC,所以可移植性不是一个大问题,但这是不可移植的。 I remember back to a very interesting object oriented system I used for the one game I wrote that published on the Sega Genesis. 我记得回到一个非常有趣的面向对象系统,该系统用于我在Sega Genesis上发布的一款游戏。 This was all done in 68K assembler, back in about 1995, including a V-table. 大约在1995年,所有工作都在68K汇编器中完成,其中包括一个V表。 Due to interesting memory constraints, all V-tables lived miles away from their class instances, in a special area of the first 64K page of memory. 由于有趣的内存限制,所有V表都在其前64K页内存的特殊区域中与类实例相距数英里。 We had some gnarly linker trickery to hook everything up and make it all work. 我们有一些棘手的链接器技巧,可以将所有内容连接起来并使其正常工作。

My point being that your line of code to go and fetch VMT may not wind up pointing at the V-table at all. 我的观点是,您要获取VMT的代码行可能根本无法指向V表。 The workings of this are implementation dependent, the compiler writer is at liberty to do it any way they want, as long as the end product works correctly according to the standard. 它的工作方式取决于实现,只要最终产品根据标准正确运行,编译器编写者就可以按照自己想要的任何方式进行操作。

And the final question? 还有最后一个问题? Why? 为什么? What problem are you trying to solve that requires digging around like this in places most programmers stay well away from. 您要解决什么问题,需要在大多数程序员都远离的地方像这样挖掘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM