Reputation: 901
I am sorry to ask this trivial question but I could not find a definitive answer: if I have explicit static initialization to zero, is it zero initialization or initialization with constant expression? Say if I have
a.hpp:
class A { ... static int x; }
a.cpp;
int A::x = 0;
How many times will 0 be assigned to x
? Once during zero initialization or twice during both zero initialization and initialization with constant expression?
Upvotes: 2
Views: 423
Reputation: 153899
The value of the variable will be 0 before any of your code is executed. How it gets that way depends largely on the system; one typical approach is to read an image of the date from disk, when loading the program. Formally, you have zero initialization, followed by static initialization, but there's no way a conforming implementation can tell, and I've never heard of an implementation that separates the two.
Under Unix, at least in its older and more traditional versions, uninitialized static variables were placed in the bs segment, statically initialized variables in the data segment. The executable file on the disk contained an image of the data segment, which was copied into memory; all of the bytes in the bs segment were set to 0. On a modern machine, with paged virtual memory, I would expect similar behavior, with the difference that the initialization be deferred until the page was first accessed.
I would be very surprised if Windows handled this differently (except for the names of the segments).
Upvotes: 2