-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?

7
  • 5
    x is uninitialized. You shouldn't have any expectations to its value. (This is U.B., strictly speaking.) So, the answer could be "Why not." ;-) Commented Mar 13, 2019 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. Commented Mar 13, 2019 at 13:46
  • Try initializingx with a value. e.g. int x = 1 Commented Mar 13, 2019 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? Commented Mar 13, 2019 at 13:53
  • 1
    Is there a general-purpose duplicate for "why is my undefined behaviour behaving undefinedly" questions? Commented Mar 13, 2019 at 14:09

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 I doubt there's anything about writing fast programs to be learned about this.
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.

1 Comment

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.
-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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.