-1

Please, consider the following C++ snippet:

#include <iostream>

int main() {
    int x;
    std::cout << x << '\n';
    return 0;
}

as expected the result printed will be unpredictable since the variable x has not been initialized. If you run it may get 458785234 and 348934610 the second time. However, if you change the code the following way:

#include <iostream>

int main() {
    int x;
    std::cout << x << std::endl;
    return 0;
}

Now, the x printed is always equal to zero. Why is that? Note the only change introduced is the std::endl. Can anybody explain why this assigns 0 to x variable?

user3732445
  • 241
  • 1
  • 10
  • 5
    `x` is uninitialized. You shouldn't have any expectations to its value. (This is [U.B.](https://stackoverflow.com/a/4105123/1505939), strictly speaking.) So, the answer could be "Why not." ;-) – Scheff's Cat Mar 13 '19 at 13:42
  • *"Now, the x printed is always equal to zero."* Only as far as you've tested it. It's undefined behavior, so it could act differently on other platforms, with other compilers or for no apparent reason. – François Andrieux Mar 13 '19 at 13:46
  • Try initializing`x` with a value. e.g. `int x = 1` – edtheprogrammerguy Mar 13 '19 at 13:50
  • 1
    Interesting (insofar that looking at undefined behaviour is interesting). What does the generated assembly tell you about it *always* being zero in the second case? – Bathsheba Mar 13 '19 at 13:53
  • 1
    Is there a general-purpose duplicate for "why is my undefined behaviour behaving undefinedly" questions? – Useless Mar 13 '19 at 14:09
  • @Useless duplicate? https://stackoverflow.com/q/54120862/5470596 – YSC Mar 13 '19 at 14:12
  • it is a compiler dependent thing as far as I see – user3732445 Mar 13 '19 at 15:06

3 Answers3

3

as expected the result printed will be unpredictable ...

Now, the x printed is always equal to zero. Why is that?

It is so because the behaviour is undefined.

You expected the number to be "unpredictable". It seems that you didn't predict the number to be zero. This should be according to your expectations.

You did nothing to make the number non-zero, so why would you expect the number to be non-zero?

On the other hand, you may have been expecting that the behaviour doesn't change because the change to the program seems to be unrelated. That expectation is ill-advised. Undefined behaviour is not guaranteed to be the same undefined behaviour if you change any part of the program. In fact, the behaviour is not guaranteed to be the same even if you don't change anything. On the other hand, the behaviour is also not guaranteed to be different. Nothing about the behaviour of the program is guaranteed. That is what undefined behaviour means.

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • Thanks you! The intention behind my question was to find the mechanism that led to x being 0. I am trying to learn and write fast programs so I need to know about all the details how it got set up. – user3732445 Mar 13 '19 at 15:05
  • 1
    @user3732445 I doubt there's anything about writing fast programs to be learned about this. – eerorika Mar 13 '19 at 20:46
2

With gcc 5.4.0 on Ubuntu 16.04 I'm getting 0 in both versions of your code. But that doesn't matter since x is uninitialized and trying to read it is undefined behavior. Anything may happen depending on the particular compiler and system being used with no guarantee for any particular behavior.

Now consider the following:

#include <iostream>

void foo() {
    int x;
    std::cout << x << std::endl;
}

void bar() {
    int y = 123;
    std::cout << y << std::endl;
}

int main() {
    foo();
    bar();
    foo();

    return 0;
}

On my machine it prints:

0
123
123

So my guess is that my compiler does zero initialization of stack area before program starts but doesn't bother to do so later to avoid unnecessary works.

But as I pointed out before, these behaviors are undefined. There is no requirement from standard regarding this and as a result we must not assume anything specific to happen always.

taskinoor
  • 45,586
  • 12
  • 116
  • 142
  • Yes, it might be the compiler issue. Because on my Mac with g++ (Apple LLVM version 10.0.0 (clang-1000.10.44.4)) the behavior is different. – user3732445 Mar 13 '19 at 15:06
-1

It's undefined behavior what you get is compiler call With Microsfot Visual C++ it doesn't even compile (error C4700: uninitialized local variable 'x' used)

Soulimane Mammar
  • 1,658
  • 12
  • 20