visual-studiovariablesinitialization

Visual studio 2005: is there a compiler option to initialize all stack-based variables to zero?


This question HAS had to be asked before, so it kills me to ask it again, but I can't find it for all of my google and searching stackoverflow.

I'm porting a bunch of linux code to windows, and a good chunk of it makes the assumption that everything is automatically initialized to zero or null.

int whatever;
char* something;

...and then immediately doing something that may leave 'something' null, and testing against 'something'

if(something == NULL)
{
.......
}

I would REALLY like not to have to go back throughout this code and say:

int whatever = 0;
char* something = NULL;

Even though that is the proper way to deal with it. It's just very time consuming.

Otherwise, I declare a variable, and it's initialized to something crazy if I don't set it myself.


Solution

  • This option doesn't exist in MSVC, and honestly, whoever coded your application made a big mistake. That code is not portable, as C/C++ say that uninitialized variables have an undefined value. I suggest setting the "treat warnings as errors" option and recompiling; MSVC should give you a warning every time a variable is used without being initialized.