-1

I was trying to test something regarding dangling reference in c++, and came up with the following code

#include <iostream>

using namespace std;

int& get_int_ref() {
  int var = 16991;
  int &var_ref = var;
  return var_ref;
}

int main() {
  cout << get_int_ref() << endl;
  return 0;
}

When I tried to compile this program with clang (clang-1000.10.44.4), I rightly got the dangling reference warning:

warning: reference to stack memory associated with local variable 'var' returned [-Wreturn-stack-address]
  return var_ref;
         ^~~~~~~

With gcc (4.8 and 7.3.0), I didn't even get a warning. When I tried to run the executable, it prints the correct value (and not a garbage value) in all cases (with all different compilers).

$ ./a.out
16991

Clearly there is something that I am missing here. Would you agree that I shouldn't be doing this, and can someone point out what is the problem here?

Nishant
  • 409
  • 6
  • 16
  • 4
    Undefined behavior means anything can happen, not that what you expected won't happen. One of the worst things about undefined behavior is it often looks like your code works as intended, until it stops working later when you won't suspect the code you've written weeks ago. – François Andrieux Jan 07 '19 at 20:06
  • 2
    g++ will catch that mistake and provide a warning if you turn the optimizer on. My guess is the debug build doesn't look at it close enough to spot the mistake. – user4581301 Jan 07 '19 at 20:10
  • 3
    And I know a guy who crossed a street at red light and nothing happened - he safely landed on the other side of the street. Why nothing did happen, when they said it is not allowed? – SergeyA Jan 07 '19 at 20:10
  • 1
    Try printing it twice in a row. The original value will get overwritten – Alecto Irene Perez Jan 07 '19 at 20:14
  • @JorgePerez - You are right, printing it out twice overrides the original value. Do you know why this happens ? – Nishant Jan 07 '19 at 22:48
  • Any local variables are allocated on the stack. This happens by the stack pointer being shifted to make room for them. When a block of code ends, the stack pointer is shifted back to where it was before the start of the block of code. This "deallocates" any local variables, but it doesn't clear the memory where they were stored. When you return a reference to a local variable, that memory hasn't been cleared so it gives you the correct value. Calling a different function (e.g. the second print statement) might then reuse the deallocated stack space, trampling over the old value. – Alecto Irene Perez Jan 07 '19 at 22:56

1 Answers1

2

it prints the correct value (and not a garbage value)

No. It prints a garbage value. There is no correct value for a dangling reference.

Clearly there is something that I am missing here.

What you're missing is that the behaviour is undefined.

Printing it out twice overrides the original value. Do you know why this happens ?

It happens because the behaviour is undefined.

eerorika
  • 232,697
  • 12
  • 197
  • 326