4

I wonder why this C++ code is valid and doesn't cause any errors:

extern int B;
int A = B;
int B = A;

int main()
{
    printf("%d\n", B);
    system("pause");
    return 0;
}

First, the variable A will be created in some memory address, then its value will be initialized from variable B, but then variable B goes back to initialize its value from variable A, and so on, ...
So, why is there no infinity loop or any error here?
The program still runs ok, and the value of B is 0

This is valid for Java as well:

class A {
    static final int AA = B.BB;
}

class B {
    static final int BB = A.AA;
}

Any one can explain these questions for me, thanks!

Tim Schmidt
  • 1,297
  • 1
  • 15
  • 30
phibao37
  • 2,230
  • 4
  • 26
  • 35
  • 3
    Be careful here. Even if the code might seem similar and *seem* to behave in a similar way, Java and C++ are still very different languages and might in reality behave very differently for otherwise similar code. – Some programmer dude Nov 08 '16 at 07:50
  • Heck, this is an area where C and C++ start to differ. – MSalters Nov 08 '16 at 08:16

2 Answers2

4

Since I´m not familiar with c++ I can only explain it to you with the java example.

I think this might explain the issue:

class A {
    static final int AA = B.BB;
}

class B {
    static final int BB = A.AA;
}
  • A.AA gets initialized with value 0

  • A.AA looks for B.BB

  • B.BB gets initialized with value 0

  • B.BB looks for A.AA

  • At this time A.AA already has the value zero (default value of int), so B.BB becomes 0.

  • A.AA becomes 0

Tim Schmidt
  • 1,297
  • 1
  • 15
  • 30
  • Java variables start with a default value and are then assigned +1 – Peter Lawrey Nov 08 '16 at 07:54
  • thanks for your explanation about Java (still, I can't make 2 acceptances for 2 answer, sorry for that :)). – phibao37 Nov 08 '16 at 08:29
  • So, the main key is that if a variable (`BB`) is looking for another (`AA`), there are 2 cases: **1.**_if `AA` is not initalized_, it will init this variable with default value, looking for assign value if have and assign back to `BB`. **2.**_if `AA` is already initalized_, it will assign back the value of `AA`, no matter if `AA` is looking for someother or not – phibao37 Nov 08 '16 at 08:29
  • `no matter if AA is looking for someother or not` I´d rather say, that the compiler doesn´t recognize or even care if AA still is looking fo a value, it just finds the current value of AA, which is 0. – Tim Schmidt Nov 08 '16 at 08:48
3

I am answering this for C++. Although the story might not be all that different for Java

It is not an infinite loop because everything is resolved at compile time, and here is how:

  • The compiler sees that B is declared as extern
  • The linker knows that A has to be set to the value of whatever B is supposed to be when it is declared, so setting the value of A is delayed until much later
  • B is finally declared but since it is not assigned a value, it gets a default value of 0.
  • The linker finally resolves the value of A and can now set it to 0 as well.
  • Compiler compiles your program and the output is 0

See this answer for more details

Community
  • 1
  • 1
smac89
  • 39,374
  • 15
  • 132
  • 179
  • "B is finally declared but since it is not assigned a value". So, does it mean that `B` will looking for `A` but it see that `A` is delayed waiting for something else, so `B` will ignore init from `A` and set from default value (0) ? – phibao37 Nov 08 '16 at 07:58
  • 1
    @user2447581 this will be going into details of how the compiler works, but essentially once the value of `B` is discovered to refer to itself, then I believe the compiler will detect a cycle in internal graph it uses to keep track of what variables are used where, and when it does it will break the cycle and use the default value of `B` which `A` then gets – smac89 Nov 08 '16 at 08:02