指针未正确返回值

pointer not returning the value correctly

本文关键字:返回值 指针      更新时间:2023-10-16

我在程序中返回指针的值时遇到问题,指针的值未保存,并且当它读取时返回 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。