Compiler Switch to Init all variables

Started by
7 comments, last by MetaKnight 19 years, 10 months ago
Does anyone know the sompiler switch to Init all variables? or how to do it in visual studio .net? I know it''s on by deafult for debug, but i want to add it to release. Thank you
Advertisement
That the certain compilers initialize certain variables to certain values in certain situations is purely a debugging aid.

Exactly what do you want the compiler to initizlse your variables to? A random value? INVALID_HANDLE_VALUE? 0? 0xFFFFFFFF? The answer is that the C++ standard doesn''t tell, so the compiler has no defined way of initializing variables.

Do the initialization yourself.
Initialization should be done in your constructor, or when the variable is declared. Don''t rely on your compiler for that. What if the compiler initializes it to something you don''t want?

---------------------------Hello, and Welcome to some arbitrary temporal location in the space-time continuum.

Turn on compiler warnings, and then fix them. Make the compiler treat warnings as errors, and not actually complete until there are no warnings. The compiler will warn about use of uninitialized variables, when the right switches are turned on.

Warnings are your friends. Almost as good friends as the debugger.
enum Bool { True, False, FileNotFound };
That actually reminds me of an interesting piece of code I saw once where the top of the header looked like this:

#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)#pragma warning(disable : xxxx)


Why? Because the original programmer thought the warnings were "stupid". Oi.

---------------------------Hello, and Welcome to some arbitrary temporal location in the space-time continuum.

Not necessarily. There are some warnings generated by MSVC that are caused by their STL code (and possibly in other things too). Some of these warnings can be very annoying because they''ll show up in huge quantities or be really long messages so they''ll make it tougher to find the compiler errors in the list.

The particular offender I can think of off-hand is std::map. It throws warnings about symbol names being too long (because there''d be several indirections through classes, all of them requiring template parameters and so on) and once a symbol exceeds 255 characters (I think) the compiler will spit out a warning.

Changing the warning level can help but will disable other warnings you might actually want to see, so you can disable individual warnings to hide warnings like those and still see the ones you like. Though my std::map example isn''t a great one because the compiler seems to ignore the #pragmas that disable those specific warnings (it''s a known bug and I don''t remember if they fixed it in a service pack).

That said though, there''s still the possibility that that coder just didn''t want to see any warnings and disabled everyone he was getting instead of disabling them altogether...

-Auron
quote:Original post by Auron
Not necessarily. There are some warnings generated by MSVC that are caused by their STL code (and possibly in other things too). Some of these warnings can be very annoying because they''ll show up in huge quantities or be really long messages so they''ll make it tougher to find the compiler errors in the list.
Somewhat true. However with VC++ 7.1 and the latest STL this has been fixed.

Even with VC6 and the poor STL version shipped with that, you can have a single header file (such as stdafx) and disable warnings only for stl/windows header files, but compiler your own code on W4 without warnings. It''s probably a pain to fix for an existing project, but for a new project it''s very doable and strongly recommended.
quote:Original post by Anonymous Poster
Even with VC6 and the poor STL version shipped with that, you can have a single header file (such as stdafx) and disable warnings only for stl/windows header files, but compiler your own code on W4 without warnings. It''s probably a pain to fix for an existing project, but for a new project it''s very doable and strongly recommended.


You can also re-enable warnings with #pragma in VC after they have been disabled.
well my code is fine, i run with warning level 4(the highest) and i fix them all, but- im usign a physics library called tokamak, and in release mode, everything get's messed up. I just wanted to turn that on and see if that was the problem. I never compile with a warning, unless it's not from me.

[edited by - MetaKnight on June 6, 2004 7:13:12 PM]

This topic is closed to new replies.

Advertisement