Hello,

So I've got a code where I'm wanting to make sure that there is nothing in this class variable "finalgrid". To do this I've got an if statement:

if(canvas->finalgrid)
OR
if(finalgrid)

Sorry for the confusion with the two statements, but I'm working on modifying one project to fit in another, (wrapping a glui project with wxWidgets). Unfortunately, the gluiproject without the canvas-> works properly and appears to be the same as the modified project, but when I put a break point at the if statement, this is what I see as a discrepancy.

canvas->finalgrid = 0xcdcdcdcd {BoundaryVertices=??? GridSquareVertices=??? GridSquareFlag=??? ...}

finalgrid = 0x00000000 {BoundaryVertices=??? GridSquareVertices=??? GridSquareFlag=??? ...}

I didn't think that with either of their contents, either should enter the if statement, but actually the canvas->finalgrid does enter it, and I'm guessing it might have something to do with 0xcdcdcdcd but I'm not sure. The finalgrid does not enter the if statement, and I'm not sure why its address is 0x0000000.

If anyone can shed some light onto this situation, I would be delighted! Thanks in advance and if I didn't make anything clear enough please let me know.

Note: these were instantiated as dynamic instance of another class

GridGeneration *finalgrid;

I'm not initializing it, so I'm not wanting anything to be in the variable, meaning I do NOT want it to enter that if statement, but unfortunately it is in the case of (canvas->finalgrid).

Dani AI

Generated

As already pointed out, the debugger was showing a debug-fill pattern for an uninitialized pointer. The important thing to note is that an if test on a pointer does not check for "valid object" — it simply converts the pointer to a boolean. Any nonzero pointer value evaluates as true, so a pointer filled with a debug pattern will make the if succeed even though it doesn't point to a real object.

Two practical facts to keep in mind: variables with static or global storage are zero-initialized by the runtime, so they can appear as 0x00000000 (null). Members or local variables that are not explicitly initialized have indeterminate contents; debug builds often fill those with recognizable bytes to help you find bugs, but that does not make the behavior defined. Relying on debugger fill patterns is dangerous, because release builds will not do the same fill and the pointer can contain any garbage (undefined behavior).

Fixes and best practices:

  • Always initialize pointer members in-class or in the constructor. In modern C++, prefer smart pointers to manage ownership and to be null-safe by default.
  • Set pointers to null after deleting them if you continue to use the variable.
  • Use tools (AddressSanitizer, Valgrind, or Visual Studio CRT debug heap checks) to find reads of uninitialized memory.

Example (modern C++ approach):

#include <memory>

class Canvas {
    std::unique_ptr<GridGeneration> finalgrid; // default-initialized to null
    // use std::make_unique<GridGeneration>(...) when you need to allocate
};

If switching to smart pointers is not possible, make sure each pointer member is explicitly initialized (constructor or in-class) and avoid testing or dereferencing it before initialization.

Recommended Answers

All 2 Replies

When you compile programs with Microsoft compilers in DEBUG mode the compiler will fill pointer addresses with that value 0xcdcdcdcd or some such value like that to indicate that address has not been used. if you code something like this char *ptr = 0; then the address will be 0x0000, as expected, but char* ptr the address is set to 0xcdcdcdcd.

If you are going to test the validity of that pointer, then declare it like this: GridGeneration *finalgrid = 0; or GridGeneration *finalgrid = NULL; , both are equivalent.

Well that makes perfect sense, but could you explain why even without being initialized, just the filled pointer address it was entering the loop?

Or does it just do that because?

And thanks for the solve :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.