Lets say that we have two compilation units as follows:
// a.cpp
extern int value2;
int value1 = value2 + 10;
// b.cpp
extern int value1;
int value2 = value1 + 10;
When I tried it on VC2010, it initializes value1
and value2
to zero first. aren't both value1
and value2
dynamically initialized and default initialization doesn't apply on them?
Thanks,
3.6.2/1 says that "Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place".
So you're right, they aren't default-initialized. But they are zero-initialized, which in fact for int
is the same thing. For a class type it's not necessarily the same thing.
That said, I'm not promising the behavior here is merely that the order of initialization is unspecified, and hence that one variable ends up as 10 and the other 20, but unspecified which is which. It might be undefined on some other grounds, but I can't think of any.