Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug uninitialized variables in VC++

Tags:

c++

How to debug uninitialized variables in release mode in C++.

like image 993
brett Avatar asked Oct 28 '25 19:10

brett


2 Answers

There's a warning for this. You should try to always compile cleanly at the highest warning level. For VC++ this is level 4. Turn off specific annoying warnings selectively only.

Also, unless you deliberately uncheck the option, VC++ will compile with /RTCu (or even /RTCsu) which puts in checks to catch uninitialized variables at run-time.

Of course, proper programming style (introduce variables as late as possible) will prevent such errors from happening in the first place.

like image 174
sbi Avatar answered Oct 31 '25 09:10

sbi


Generally, rather than debugging uninitialized variables, you want to prevent the very possibility, such as using classes/objects with ctors, so creating one automatically and unavoidably initializes it.

When you do use something like an int, it should generally be initialized as it's created anyway, so uninitialized variables will be pretty obvious from simple inspection (and you generally want to keep your functions small enough that such inspection is easy).

Finally, most decent compilers can warn you about at least quite a few attempts at using variables without initialization. Clearly such warnings should always be enabled. One important point: these often depend on data-flow analysis that's intended primarily for optimization, so many compilers can/will only issue such warnings when you enable at least some degree of optimization.

like image 31
Jerry Coffin Avatar answered Oct 31 '25 09:10

Jerry Coffin