1

I want to understand this snipped:

#include <iostream>

int& GetInt();    
void Print(int& valueToPrint);

int main()
{
    int& myValue = GetInt();
    Print(myValue);
}

int& GetInt()
{
    int x = 9;
    return x;
}


void Print(int& valueToPrint)
{
    std::cout << valueToPrint;
}

I was expecting this to print out 9, instead I get some high integer value of 32759. In my understanding, the function GetInt returns a reference to the variable myValue. I then pass this reference as a reference to the function Print.

My guessing is that this isn't working because x is defined inside the scope of GetInt and removed from stack memory after exiting this function. But why does my reference then get a strange random big integer number?

Tobias von Falkenhayn
  • 1,355
  • 5
  • 26
  • 59
  • 1
    `x` no longer exists when you return = undefined behavior. – Captain Obvlious Jan 05 '23 at 06:58
  • 1
    UB means anything is possible. – songyuanyao Jan 05 '23 at 06:58
  • 1
    Why would `GetInt` return a reference to the variable `myValue`? It doesn't know anything about `myValue`. The opposite is the case: `myValue` is a reference to whatever `GetInt` returns. In this case, what it returns is a reference to its own local variable `x`, which immediately becomes invalid as soon as `GetInt` has returned. Your compiler [should be warning you](https://godbolt.org/z/PK533dWx5) about this. – Nathan Pierson Jan 05 '23 at 06:58
  • 2
    [Does "Undefined Behavior" really permit *anything* to happen?](https://stackoverflow.com/questions/32132574/does-undefined-behavior-really-permit-anything-to-happen) – Some programmer dude Jan 05 '23 at 07:00
  • There's even a name for the leftover reference to nowhere: it's called a _dangling reference_. – Michaël Roy Jan 05 '23 at 07:00
  • 1
    What did you expect to happen instead? C++ does not require any particular behaviour because doing so would require the compiler to add some kind of check for an invalid reference. This would slow all programs down, valid or invalid. – john Jan 05 '23 at 07:06
  • 1
    Good reading on the subject: [Can a local variable's memory be accessed outside its scope?](https://stackoverflow.com/q/6441218/4581301) – user4581301 Jan 05 '23 at 07:08
  • 1
    *But why does my reference then get a strange random big integer number?* -- Even if it got `9`, the code is still wrong. A `9` is just as random as `432433` or `0`. This is why writing "proof of concept" programs to see what happens when you do something illegal just doesn't work with C++. You could have gotten a "good" value, and you would have thought that things are ok, when they are not ok. The bottom line is that knowing when the code will cause undefined behavior is something that you either know or you don't know, i.e. knowledge achieved through experience. – PaulMcKenzie Jan 05 '23 at 08:05
  • @john well, I was expecting either 9 (meaning the variable will be kept in memory since the compiler knows its still referenced by a reference) or an exception during runtime. – Tobias von Falkenhayn Jan 05 '23 at 11:06
  • @TobiasvonFalkenhayn The first does not happen. C++ does not keep alive data just because something else is referencing it. The second might happen, but it is not required. The technical term for this situation is *undefined behaviour* which means what it says. C++ does not impose any particular behaviour on this situation and it is wrong to expect any particular behaviour. This is how C++ is, other languages differ. Both your statements suggest a background in Java, if so then you really need to forget almost everything you know about Java when learning C++. – john Jan 05 '23 at 11:46
  • @TobiasvonFalkenhayn *I was expecting either 9* -- And this makes my comment even more relevant. If you got a `9`, you would have thought that "yes, C++ holds onto the reference" and then probably go on to write more sophisticated code believing this to be a fact -- but you would have been completely wrong. Then what usually happens is a back and forth between experienced C++ developers and new programmers, where the new programmer insists that the experienced person is wrong because "they wrote a program that returns the right number". – PaulMcKenzie Jan 05 '23 at 15:39

0 Answers0