0
#include<iostream>
#include<conio.h>

using namespace std;

 /* test class: created the reference of 
    abc class locally in function getRef() 
    and returned the reference.*/

class abc {
    int var;

    public:
        abc():var(5) {}
        abc(int v):var(v) {}
        abc & getRef() {
            abc myref(9);
            return myref;
        }
        void disp() {
            cout<<var<<endl;
        }       
};

int main() {
    abc a;
    abc b=a.getRef(); 
    b.disp();  /* this statement executed perfectly. 
               I think compiler should throw error here. */
    getch();
    return 0;
}

Compiler should throw compilation error. Please explain ?

Jon Taylor
  • 7,865
  • 5
  • 30
  • 55
Shashank Jain
  • 469
  • 1
  • 5
  • 11
  • 1
    possible duplicate of [Can a local variable's memory be accessed outside its scope?](http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope) – Bo Persson Jul 26 '12 at 11:02

2 Answers2

1

The compiler shouldn't flag b.disp(); as an error, as that isn't where the error is. The error is at return myref;, and the reason this isn't a hard error is that it is very tough in general to determine whether an object's lifetime will have ended after the return. In this case, it's easy, and some compilers do try to warn about it. Check your warning level.

Edit: with gcc, by the way, the warning is enabled by default and looks like "warning: reference to local variable '...' returned".

  • If there is an error during return of myref. then why i am getting output at terminal as 9. Output should have garbage value accroding to me – Shashank Jain Jul 26 '12 at 10:28
  • @ShashankJain What makes you think "garbage" won't be 9? The behaviour is not defined by the C++ standard, and that means *any* behaviour at runtime is valid, including the "right" behaviour. (In practical terms, the "garbage" is loaded from the same memory location that you previously wrote to, and happens by chance to not have been overwritten yet.) –  Jul 26 '12 at 10:37
0

G++ will warn you about this appropriately, but it shouldn't error and certainly not at b.disp();, since you're not actually using the return value on that line and you copied it on the line above.

: In member function ‘abc& abc::getRef()’:
:17: warning: reference to local variable ‘myref’ returned

If your compiler doesn't warn you about this, then check your warning level.

Also:

#include<conio.h>

This does not make for portable code.

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • but my question is, if this is wrong and your compilter throws warning too (mine is visual stuido 2008 and not throwing any warning) , then why does I am getting output as 9. I am using the variable store in object b to get it displayed over the terminal. – Shashank Jain Jul 26 '12 at 10:27
  • @ShashankJain: I've updated my answer to include a link to "/w, /Wn, /WX, /Wall, /wln, /wdn, /wen, /won (Warning Level)". – johnsyweb Jul 26 '12 at 10:33