Just a quick one about why it’s important to initialize variables in code. Today I spent some time on finding a simple bug. The C++ debugger kept bugging me that I attempted to delete unallocated memory. Big nono:)! Anyway I am sorta familiar with debugging and C++.
So the first thing I looked at was what was being deleted. For developers not using C++ in C you have to manually delete allocated memory or you’ll get the dreaded memory leaks :(. What caught my eye was the memory address being deleted. It was 0xDCDCDCDC, which to my knowledge means the debugger never allocated it nor was it initialized.
This happened because I used a simple struct with three string objects (home made). In the destructor of the struct I did delete all the string objects. The function I use for this checks to see if the objects aren’t already NULL to prevent deleting non existing memory.
What I failed to do was initialize all variables to NULL in a constructor, which in most languages happens by default. Me working in more then one language forgot that in C++ no initialization is standard. So when debugging the debugger initializes to something it recognizes. But in a non-debug version you never now what is in the variable.
After fixing my constructor everything worked again. Thank GOD for that!