简体   繁体   English

C ++:帮助理解这一行代码

[英]C++: Help understanding this line of code

I was looking for a way to access the vtable directly through a pointer and came across this post: http://www.codeproject.com/Tips/90875/Displaying-vtable-when-debugging 我一直在寻找一种直接通过指针访问vtable的方法,并且遇到了这篇文章: http : //www.codeproject.com/Tips/90875/Displaying-vtable-when-debugging

It works fine and I can invoke functions through the vtable entries. 它工作正常,我可以通过vtable条目调用函数。 But I'm having trouble understanding this line of deo: 但是我在理解这行deo时遇到了麻烦:

void (**vt)() = *(void (***)())ptr;

ptr is a pointer to an object. ptr是指向对象的指针。 How can I decipher this line and how does vt get the vtable address? 我怎样才能破译这行? vt如何获得vtable地址?

Thank you. 谢谢。

The actual location of the _vtable in an object is compiler dependant. _vtable在对象中的实际位置取决于编译器。 The code assumes that the _vtable address is stored first in the object (which all modern compilers seem to do). 该代码假定_vtable地址首先存储在对象中(所有现代编译器似乎都这样做)。 Also, there is only one vtable per class, which is stored in a single location. 而且,每个类只有一个vtable,它存储在一个位置中。 Thus, every instance has an address to that array of pointers to functions. 因此,每个实例都有一个指向函数的指针数组的地址。

Basically, you create a variable named vt, that is an array of pointers to functions that return void and take no parameters. 基本上,您将创建一个名为vt的变量,该变量是指向返回无效且不带参数的函数的指针的数组。 Then you initialize this variable with the content found in the first member of the object (the adress of the vtable). 然后,使用在对象的第一个成员(vtable的地址)中找到的内容初始化此变量。

(void (***)())ptr

means that you cast this first 4 (32 bit machine) or 8 (64 bit machine) to a pointer to an array of function pointers that return void and take no parameters. 表示您将这第一个4(32位计算机)或8(64位计算机)强制转换为指向返回无效且不带参数的函数指针数组的指针。

*(void (***)())ptr

returns the content of those 4 or 8 bytes, that is the address of the vtable. 返回这4或8个字节的内容,即vtable的地址。

void (**vt)()
vt is a pointer to a pointer to some function. vt是指向某个函数的指针。 This function returns void and takes no parameters. 该函数返回void并且不接受任何参数。
void (***)()
means a pointer to pointer to pointer to such a function (ie. one pointer level more than above). 表示一个指向指向该函数的指针的指针(即比上面的指针高一个指针)。

First, you´re casting ptr to such a three-level-pointer-to-function. 首先,您将ptr转换为功能的三级指针。
Then, you get the thing it points to (with the single * ), that is a two-level-pointer-to-function, ie. 然后,得到它指向的东西(带有单个* ),即指向函数的两级指针。 the same type a the variable vt. 相同类型的变量vt。 That means you can assign it to vt, and that´s exatcly what this line does. 这意味着您可以将其分配给vt,这恰好是这行的功能。

And why? 又为什么呢?

A single function pointer to a void-no-param-function can be stored in such a variable x: void(*x)() . 指向void-no-param-function的单个函数指针可以存储在这样的变量x: void(*x)() And not exactly related, if you want to pass some array to a function, you´re passing a pointer. 并非完全相关,如果您要将某个数组传递给函数,则需要传递一个指针。 Ie. 就是 it´s enough to store the array address in a pointer to access the whole array (if the length is known). 只要将数组地址存储在指针中就可以访问整个数组(如果长度已知)就足够了。
That means void(**vt)() can store (the address of) an array of function pointers. 这意味着void(**vt)()可以存储函数指针数组(的地址)。
The vtable is nothing else, just an array of function pointers. vtable只是函数指针的数组。

If you have a class Car with some variables like color and a vtable, and it uses eg. 如果您有一个Car类,其中包含一些变量,例如color和vtable,则它使用例如。 200 byte per object. 每个对象200个字节。 A pointer to a car is like a pointer to a 200-byte array, and if you access color in the code, the compiler looks up that color is at eg. 指向汽车的指针就像指向200字节数组的指针,如果您访问代码中的颜色,则编译器将查找该颜色为例如。 byte 133 and generates an access to it. 字节133并对其进行访问。

Where the vtable is within this object is implementation defined, but often at the beginning. vtable在此对象内的位置是实现定义的,但通常是在开始时定义的。 If you have a pointer to a car, it´sa pointer to a array of 200 byte, separated into some fucntion pointers first and some other data after them ... 如果您有指向汽车的指针,则它是指向200个字节的数组的指针,该指针首先分成一些功能指针,然后又分成一些其他数据...

=> The vtable, ie. => vtable,即 an array of function pointers, starts at the start address of the car. 一个函数指针数组,从汽车的起始地址开始。
=> If you cast a car pointer to a pointer to vtable, ie. =>如果将汽车指针转换为指向vtable的指针,即 pointer to "function pointer array", ie. 指向“函数指针数组”的指针,即 pointer to "pointer to pointer to function" and deference that, you have the vtable array. 指向“指向函数的指针”的指针,并尊重您拥有vtable数组。

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

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