Initializing static variables

I heard a rumor (fellow programmer) that static variables automatically initialize to 0.
static float MyFloat;

Is this true or do I need to manually initialize it to 0.f?
Why take the risk?
I'm not writing code, I'm debugging someone else's code. I just want to check if this may be the issue although he claims it isn't.
I'm not very experienced in static variables, but since nobody has answered yet, I'll just throw in my limited knowledge:

On my compiler (VS2010's compiler), Global variables are initialized as 0, but only when running in Release mode. If I understand statics correctly, they share a lot of characteristics with global variables, so it's entirely possible that they're initialized to 0 as well. Which brings me back to my previous post: why take the risk? Statics can be initialized pretty much anywhere, so I see no reason to leave it implicit. It's the only way to guarantee you get the behaviour you want, and it makes your code more transpartent.
As this is not an issue dealt on standard basis it entirely depends on the implementation of you compiler.
So as @Gaminic said why take the risk?

To be safe on this I would suggest to considered it not initialized unless proven wrong so in your case check for this possibility.
This is always true and is guaranteed by the C++ language standard.

C++03 §3.6.2[basic.start.init]/1
Objects with static storage duration shall be zero-initialized before any other initialization takes place.

C++11 §3.6.2[basic.start.init]/2
Variables with static storage duration or thread storage duration shall be zero-initialized before any other initialization takes place.
Last edited on
Thanks for the discussion guys.

The reason I didn't want to initialize them is that they are scattered all over the solution and I'd have to make a lot of changes which make it harder to track the important changes when we look at the change logs.

Reading Cubbi's response, this explains why the original author didn't want to initialize them by default. Performance is a concern and so initializing and then re-initializing wouldn't make sense from that point of view.
Topic archived. No new replies allowed.