-1

Possible Duplicate:
Returning reference to a local variable

I happened to find this code return 5. It's OK to write it this way or should definitely be avoid?

   int& f() {

     int i = 5; 
     return i;
}

int main(){

    cout<<f()<<endl;    
}
Community
  • 1
  • 1
skydoor
  • 25,218
  • 52
  • 147
  • 201
  • Effectively a duplicate of http://stackoverflow.com/questions/1755010/best-way-to-return-early-from-a-function-returning-a-reference and related to *this posters* earlier questions here http://stackoverflow.com/questions/2474852/what-issue-is-related-with-function-that-return-a-reference-c-closed . – dmckee --- ex-moderator kitten Mar 21 '10 at 19:19

3 Answers3

10

If it works, it works only by accident. This is undefined behavior and should definitely be avoided.

The moment that f returns, there are no longer any guarantees as to what happens to the memory where i lived and what happens when you try to access it.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • 2
    Agreed. +1. You might also want to look at this: http://stackoverflow.com/questions/1755010/ – sbi Mar 21 '10 at 19:14
4

The compiler warning is correct — you can't do that. i is likely to be overwritten at some unexpected time.

Either do

int f() { // don't return reference

     int i = 5; 
     return i;
}

int main(){

    cout<<f()<<endl;    
}

or

int &f( int &i ) { // accept reference
  // actually, in the case of modify-and-return like this,
  // you should call by value and return by value without
  // using any references. This is for illustration.

     i = 5; 
     return i;
}

int main(){
    int i
    cout<<f(i)<<endl;    
}
Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • 1
    If you're modifying a by-reference parameter in place, returning the reference as well seems strange (second example). I'm not saying it's wrong, of course. –  Mar 21 '10 at 19:14
  • @Steve: Sometimes it's the thing to do. Given the example, it would appear that he wants a reference and a return value. *shrug* In the particular case of an `int`, using references is basically incorrect. – Potatoswatter Mar 21 '10 at 19:19
  • 1
    @Steve: It's exactly what happens when you overload `operator<<` for your own types. You take an `ostream&` and return the same `ostream&`. – fredoverflow Mar 21 '10 at 23:48
  • @FredOverflow - and just look at how many people really hate the stream operators. I'm not one of them, by the way, but you have to admit - they are definitely a special case in more than one way. For example, they also give a completely new meaning to old operators - not something to recommend as an everyday practice. –  Mar 22 '10 at 00:03
2

When the function 'f()' returns, the stack contents will be popped, and the memory address to the variable 'i' will no longer be valid. This code should not be used.

Kevin Sylvestre
  • 37,288
  • 33
  • 152
  • 232