I was reading about SIOF from a book and it gave an example :
//file1.cpp
extern int y;
int x=y+1;
//file2.cpp
extern int x;
int y=x+1;
Now My question is :
In above code, will following things happen ?
- while compiling file1.cpp, compiler leaves y as it is i.e doesn’t allocate storage for it.
- compiler allocates storage for x, but doesn’t initialize it.
- While compiling file2.cpp, compiler leaves x as it is i.e doesn’t allocate storage for it.
- compiler allocates storage for y, but doesn’t initialize it.
- While linking file1.o and file2.o, now let file2.o is initialized first, so now:
Does x gets initial value of 0? or doesn’t get initialized?
The initialization steps are given in 3.6.2 “Initialization of non-local objects” of the C++ standard:
Step 1:
xandyare zero-initialized before any other initialization takes place.Step 2:
xoryis dynamically initialized – which one is unspecified by the standard. That variable will get the value1since the other variable will have been zero-initialized.Step 3: the other variable will be dynamically initialized, getting the value
2.