1

Who knows why the output is such?
Although it's wrong to use a pointer like that, I would still like to understand why it behaves the way it does.

int* foo()
{
    int a=9;
    int *p=&a;
    //definitely not right to return a pointer to an invalid varible
    return p;
}
int main(int argc, char* argv[])
{
    int *p=foo();
    cout<<*p<<endl;//9
    cout<<*p<<endl;//2357228
    *p=2;
    cout<<*p<<endl;//2
    (*p)++;
    cout<<*p<<endl;//2357229
    cout<<*p<<endl;//2357228
    cout<<*p<<endl;//2357228
    (*p)++;
    cout<<*p<<endl;//2357229
    cout<<*p<<endl;//2357228

    return 0;

}
jogojapan
  • 68,383
  • 11
  • 101
  • 131
duleshi
  • 1,966
  • 2
  • 21
  • 32
  • 6
    That's the beauty of undefined behavior. – Mysticial Oct 24 '12 at 03:18
  • 2
    http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope – chris Oct 24 '12 at 03:19
  • 1
    Are you interested in the C++ language? Or are you interested in the particular code produced by your compiler in this case? If the former, the language spec has nothing to say on the subject, just don't do it. If the latter, look at the assembly output. – Benjamin Lindley Oct 24 '12 at 03:30

3 Answers3

2

Returning a pointer/reference to a variable local to an function results in Undefined Behavior. A local/automatic variable is guaranteed to be alive and valid only in the scope({,}) in which it is defined not beyond that scope.

Undefined behavior means that the program can show any behavior and it is allowed to do so by the C/C++ standards.It is meaningless to try to find reasoning of observed behavior after Undefined behavior has occured because this behavior may/may not be consistent or cannot be relied upon.

On a bright side any good commercial compiler will provide you warning about such a code.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Yes,compilers can do anything when undefined. But there must be a way that it produce the uncertainty. There exists some mechanism,right? Like what Matt has explained. – duleshi Oct 24 '12 at 03:58
  • I mean, when faced with undefined, usually one can list all-or some of- the possibilities: it produce this result via this way, and it produce that result via that way,etc. – duleshi Oct 24 '12 at 04:04
  • @duleshi: It boils down to the Q: ***Can you define something which is Undefined in itself?*** – Alok Save Oct 24 '12 at 04:08
1

Although it's wrong to use a pointer like that, I would still like to understand why it behaves the way it does.

Because p is left pointing at a location in stack memory that keeps getting written over by cout's << method. Every time you use cout, the value of p may change.

Code like this is dangerous as it can corrupt the stack and cause your program to crash.

hookenz
  • 36,432
  • 45
  • 177
  • 286
0

What you need to remember is that a and p are both scope level variables. Thus, returning the address of a variable that only exists in the stack (but is not dynamically allocated on the heap) is undefined behavior. Since we no longer know what gets written to there once it's left that address space. Ergo, returning a pointer to a local variable is undefined behavior.

M4rc
  • 473
  • 2
  • 13