对象的地址 - void* 和 void**
Address of the object - void* and void**
我试图获取存储在y中的vtable地址,但我不明白为什么x包含不同的地址。
#include <iostream>
class A {
public:
virtual int f() {
return 2;
}
};
int main() {
A obj;
void* x = (void**)&obj;
void* y = *((void**)&obj);
std :: cout << x << std :: endl;
std :: cout << y << std :: endl;
void** vtable = (void**)y;
std :: cout << ((int(*)())(vtable[0]))() << std :: endl;
}
这都是未定义的行为,但以下代码将为您提供该 vtable 地址:
A obj;
void **vtable = * (void ***) &obj;
对 vtable 的布局进行一些假设,我们可以证明是这样:
#include <iostream>
class A
{
public:
virtual void f () { }
};
int main() {
A obj;
void **vtable = * (void ***) &obj;
std :: cout << vtable << std :: endl;
std :: cout << *vtable << std :: endl;
std :: cout << (void *) &A::f << std :: endl;
}
输出:
0x400e88
0x400de4
0x400de4
多重和/或虚拟继承将使这更难做到。
现场演示
相关文章:
- 将数组的地址分配给变量并删除
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 空基优化子对象的地址
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 在c++类上调用void函数
- C++ - 非静态 void* 成员,指向具有相同地址但不同值的不同对象的另一个非静态成员
- 在带有 (void*) 强制转换的字符串中打印字符的内存地址
- 对象的地址 - void* 和 void**
- 将新放置到缓冲区后,缓冲区和实例是否具有相同的 void* 地址?
- 如何将8个字节写入void *地址
- 将地址强制转换为函数:使用 "void * void "
- 匹配布尔值与常量 void* 重载的函数地址
- 可以在C++中用void指针存储类对象的地址
- GetProcAddress取消闪烁以获取哪种形式的Void pascal Myfunction(BOOL)的地址
- 如何将地址对齐到一个void指针
- 双指针地址强制转换为void**
- 当定义一个将在内存上操作的函数时,将地址传递为void*或uint8*是否更正确?
- c++访问void*中的特定地址
- 如何在CFFI中找到指向结构的指针的地址并将其强制转换为void**
- 如何安全地将 void* 设置为特定地址 c++