static initialization order fiasco

Posted by Happy Mittal on Stack Overflow See other posts from Stack Overflow or by Happy Mittal
Published on 2010-06-14T06:47:25Z Indexed on 2010/06/14 6:52 UTC
Read the original article Hit count: 173

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;
y=x+1;  

Now My question is :
In above code..will following things happen ?
1. while compiling file1.cpp, compiler leaves y as it is i.e doesn't allocate storage for it.
2. compiler allocates storage for x, but doesn't initialize it.
3. While compiling file2.cpp, compiler leaves x as it is i.e doesn't allocate storage for it.
4. compiler allocates storage for y, but doesn't initialize it.
5. 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?

© Stack Overflow or respective owner

Related posts about static

Related posts about initialization