首页 > 解决方案 > 如何在读取或写入该对象之前检查指针指向的地址是否指向对象而不是垃圾?

问题描述

如果我有一个指针(静态地址),它总是指向一个特定的变量地址(ex.health),那么如何在程序读取和写入这个不存在的对象之前检查 game.exe 是否销毁并释放这个对象?

static DWORD baseAddress = 0x00000 ;
static DWORD healthPtr = (DWORD)(baseAddress + health_offset);

//when the game deallocates and destroys health object it's value become (??) garbage causing crash on reading or writing to it!

//So how to check here if the address pointing to health is realy pointing to a valid health object not a destroyed non sense garbage?

*(int*)(healthPtr) = 100;
// writing to a deallocated object will cause a crash

标签: c++c++11

解决方案


推荐阅读