how to avoid program crashing after reading a deallocated unknown value during runtime?


I’m using a function that compares different int values with zero.
these values represent monsters IDs surrounding the character in a 3d game

EX. if (a*(int*)monster_ID1_address == 0)

if there’s a monster the program executes normally and reads the int value of the ID and compare it to zero.

But the problem is:

if this monster is killed it’s ID value become = ?? and program can’t read this deallocated unknown value and crash!

So how to handle this and read this unknown value as zero or -1 so i could continue execution after it?

Source: Windows Questions C++