-4

I already know that the variable is destroyed when the program gets out of its scope. However, this did not seem to happen when I tried the code in the following snippet:

    int& somethin()
    {
        int x1 = 4;

        return x1;
    } 
    int main() {    
        int x11; 
        x11 = somethin();
        cout << x11 << endl;

        return 0;
    }

Surprisingly, the output is: 4

While when I declare "x11" as a reference variable, I get garbage value.

Any explanation?

Note: I repeated this test many times. I do not think it is about luck. Note: There is one who asked the same question before in StackOverflow, the answers were about the tester was lucky.

Omranovic
  • 45
  • 7
  • Think about how the execution-stack works in a machine - most C++ runtime environments do not zero-out a popped stack-frame, that's why the reference's target value `4` is still there - that doesn't mean you should depend on that behaviour, though. – Dai Jul 15 '17 at 11:25
  • [I got 0](http://cpp.sh/243to) – yrHeTateJlb Jul 15 '17 at 11:28
  • 5
    It's undefined behavior. Which means any and no specific behavior can happen. That's why it's dangerous. – StoryTeller - Unslander Monica Jul 15 '17 at 11:29
  • 1
    _"While when I declare "x11" as a reference variable, I get garbage value. "_ How did you do that? The compiler wouldn't allow uninitialized references at all? – user0042 Jul 15 '17 at 11:34
  • @user0042 it's something like this { int& x11 = somehtin(); } – Omranovic Jul 15 '17 at 11:35
  • @Omranovic I meant you should have been left with a compiler error, not _garbage value_ at runtime. – user0042 Jul 15 '17 at 11:36

2 Answers2

3

Any explanation?

The behaviour is undefined.

The standard does not guarantee that undefined behaviour would have behaviour that you expect. Neither does it guarantee that the behaviour is always the same. On my computer, then result is a segfault.

For what it's worth, you could imagine the implementation: x1 is stored somewhere in memory, so the value 4 is somewhere in memory. After the function call, that memory is no longer used for x1, so it can be used for something else. x11 is stored somewhere in memory. If same memory location happens to be used, then the garbage value in that memory location might happen to be 4.

While when I declare "x11" as a reference variable, I get garbage value.

4 is also a garbage value.

eerorika
  • 232,697
  • 12
  • 197
  • 326
1

Local variables are stored in the automatic storage. When they go out of scope, they are destroyed.

Returning a reference to a local variable violates the rules of the language and therefore is undefined behaviour (Just like as dereferencing a nullptr).

That being said there is no guarantee about the value of x11. It can produce the correct result, but that's not a language feature.

You can find more about undefined behaviour here: What are all the common undefined behaviours that a C++ programmer should know about?

KostasRim
  • 2,053
  • 1
  • 16
  • 32