指针未正确返回值
pointer not returning the value correctly
我在程序中返回指针的值时遇到问题,指针的值未保存,并且当它读取时返回 null。
标头代码:
class PlayerHK : public Player {
public:
PlayerHK();
ULONG player_hp();
ULONG player_power();
ULONG player_hp2();
ULONG player_power2();
private:
struct CPlayer
{
BYTE padding[0x20];
ULONG hp;
ULONG power;
};
CPlayer *player;
};
主代码:
PlayerHK::PlayerHK() {
player = reinterpret_cast<CPlayer*>(*reinterpret_cast<DWORD*>(0x00B1C4E5));
}
ULONG PlayerHK::player_hp() {
return player->hp; //does not return the value
}
ULONG PlayerHK::player_power() {
return player->power; //does not return the value
}
ULONG PlayerHK::player_hp2() {
player = reinterpret_cast<CPlayer*>(*reinterpret_cast<DWORD*>(0x00B1C4E5));
return player->hp; //returns the value
}
ULONG PlayerHK::player_power2() {
player = reinterpret_cast<CPlayer*>(*reinterpret_cast<DWORD*>(0x00B1C4E5));
return player->power; //returns the value
}
当我运行的程序将读取 PlayerHK 时,值不应该被保存吗?我忘了做什么吗?
如果我
正确理解了这个问题,你会问为什么
player = reinterpret_cast<CPlayer*>(*reinterpret_cast<DWORD*>(0x00B1C4E5));
在构造函数中运行时提供 NULL player
集,但在player_hp2或player_power2中运行时不提供 NULL。
显而易见的答案是,此内存位置 (0x00B1C4E5( 在构造对象时保存值 NULL,并在调用 player_hp2 或player_power2时保存不同的值。也许在构造函数运行时尚未创建播放器,因此指向播放器(您正在读取的(的指针为 NULL。
相关文章:
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 当我使用 void 函数的返回值(通过强制转换函数指针)时,究竟会发生什么?
- 如何在不使用临时变量的情况下取消引用返回指针的函数的返回值?
- 如何返回值为NULL的变量指针
- 将指向数组的指针作为函数参数传递,这本身就是另一个函数的返回值?
- 为什么 c_str() 在返回常量指针值时不输出地址
- 存储指向函数返回值的指针
- 指针未正确返回值
- 动态指针引用数组由三元运算符返回值,但有异常
- 指针返回值的地址
- 将指针设置为函数的返回值
- 即使在返回值之前释放后,数据仍在指针中仍然存在
- 返回指针、返回值或传入引用,这在C++中很优雅
- 为什么静态、非本地返回指针的返回值始终为零?
- 从共享指针矢量字符串返回值
- 当函数返回值是指针,而返回类型是c++中的引用时会发生什么
- 为什么指针返回值,而不是地址
- 返回指向常量getter的指针时,返回值类型与函数类型不匹配
- 是指向在向函数返回值/参数和类请求传输数据期间复制的内存的指针
- 作为 c++ dll 函数返回值的结构指针