views:

236

answers:

3

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.

+3  A: 

No - there's no option to do that in MSVC.

Debug builds will initialize them with something else (0xcc I think), but not zero. Unfortunately, your code is bugged and needs fixed (of course this applies only to automatic variables -for statics and globals it's fine to assume they're zero initialized). I'm surprised there was any compiler that supported that behavior - if there's an option to do that in GCC, I haven't heard of it (but I'm no expert in the dusty corners of GCC).

You may hear that an earlier version of MSVC would init variables to zero in debug builds (similar to the way 0xcc is used in VS 2005), but as far as I know that's untrue.

edit ----------

Well, I'll be damned - GCC does (or did?) have the -finit-local-zero option. Looks like it's there mostly for Fortran support, I think.

I'd suggest using compiler warnings about using uninitialized variables to help you catch 99% of your problems. I know it's not a great bit of work, but it should be done if at all possible.

Michael Burr
+1. Writing code that depends on the luck of the compiler draw is never a good thing, and when you have to ask "Is there a compiler option that does <this> to fix <that>?", you know you've got poorly written code in the first place.
Ken White
I remember an early C compiler (Lattice C?, Turbo C?) that had a "complete boolean eval" option. Even worse, a library I was using *required* this compiler switch to be true. Yikes!
Bob Kaufman
+6  A: 

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.

rlbond
Thank you guys. I just wanted to be sure there wasn't a potato peeler 3000 around before I started doing this by hand with a knife.
David
A: 

What I ended-up doing was switching to /w4. At this level, it caught most of the "yeah, that's going to be an issue" areas of initialization. Otherwise, there's nothing that can change everything from being 0xcccccccc on initialization to 0x00000000 that I saw.

Massive thanks to everyone for answering this, and yes, we will tighten it up in the future.

David