Saturday, 31 January 2015

C++ Warnings - Runtime Error - Uninitialised Variable

When we're programming, you should always look for and fix warnings, not only would this save you time in the long run, but it stops strange errors you may get at runtime.

One such error I had today was the use of an uninitialised variable, by accident, but this wasn't caught until runtime.

I missed this one because someone had set the project up with only level 3 warnings, so not only should you try to fix all warnings, but you should have your compiler spit out the highest level of warnings possible!

This can cause issues with added libraries, such as Boost, which cause lots of warnings from code you don't control but it can be confusing.  However, this does not diminish the importance of fixing errors:

The actual warning with this setting was then visible:

You can check this out with this code snippet, and check for yourselves:

#include <iostream>

int main()
int l_temp;
for (int i = 0; i < 100; i += 50)
if (i == 1)
l_temp = i;
std::cout << "temp = " << l_temp << std::endl;

No comments:

Post a Comment