0

While the title is probably confusing, I want to be able to check if an address that I'm dereferencing is a valid pointer or not. I don't mean whether or not it fits the bill of nullptr or not, but whether or not its a pointer or another type of data. The current function I have for this is

for (int player = 0;player < 31;player += 1) { // iterate each player offset, which is every 4 offsets
        
        if ((entList->bot[player] == nullptr || NULL) || (entList->bot[player]->health == NULL)) return; // checks to see if whats being accessed is null, and returns if it doesnt
        entList->bot[player]->health = 1; // trying to directly modify hp of every iteration
    }

This issue is that it wants to dereference ints, which obviously can't be done. This is the int I mean.

realized the image wasn't very helpful without context, the screenshot is of the entList array in Cheat Engine (on the right) and the value of what entList->[player] gives in Visual Studio (on the left).

So to summarize my question, how would I skip over data in a for loop that isn't of the data type I'm looking for?

  • *I want to be able to check if an address that I'm dereferencing is a valid pointer or not* -- Can't be done -- you're responsible for making sure what the pointers are pointing to are valid, and you do that by writing your code correctly. That's why C++ is one of the most difficult languages to use *if* you're going to be using pointers as a major part of your applicatio. – PaulMcKenzie Aug 11 '22 at 04:06
  • `int main() { int * p = new int; delete p; std::cout << *p; }` -- Is `p` valid when `std::cout` is called? It probably isn't a `nullptr`, but it is invalid. Imagine a larger program, where you have these types of things going on? Then it's a matter of properly debugging your application -- you can't put hacks into the code that can check if a pointer is valid or not. – PaulMcKenzie Aug 11 '22 at 04:09
  • Initialize all your pointers to nullptr at startup/object creation. Then you can check for nullptr (don't use NULL). The best way to manage pointers however is to make a design and understand how long objects will live AND thus how long pointers to them are valid so you don't need the check. An then use references where you can, those can't be null. – Pepijn Kramer Aug 11 '22 at 04:10
  • 2
    I don't think `(entList->bot[player] == nullptr || NULL)` does what you expect. – jkb Aug 11 '22 at 04:14
  • @jkb: It doesn't break anything, though. `|| false` is a no-op. – MSalters Aug 11 '22 at 09:40
  • @PepijnKramer: "Initialize all your pointers to nullptr"- No, definitely do not. Initialize them to the object they should point to. And if you don't have that object yet, don't define the pointer yet. If the object pointer to is optional, `std::optional` might be a better solution than abusing `nullptr`. – MSalters Aug 11 '22 at 09:42
  • @MSalters Could be I am a bit rusty with raw pointers, I mostly use containers of objects and references nowadays (then unique_ptr/shared_ptr, and raw pointers only for passing optional non-owning pointers and these also should never be null by design, they can always point to an object with a null strategy (reduces the amount of ifs in your code quite a lot). But if you can initialize pointers with objects they should point to then use references. – Pepijn Kramer Aug 11 '22 at 10:04

0 Answers0