-5

I am confused about the following two cases, why would one works, the other one doesn't work.

The one works:

int* test()
{
    int j=5;
    int *i = &j;
    return i;
}

the one doesn't work:

int* test()
{
    int j=5;
    return &j;
}

Both of them return a local pointer, isn't it?

Can anyone explain me the reason behind? Thanks.

Jerry
  • 323
  • 1
  • 2
  • 10
  • Yeah, I've looked it several times and I seen no difference between the two. And neither will work correctly (though they may not produce an explicit error message or crash). – Hot Licks Apr 17 '15 at 23:23
  • 4
    Returning a local variable's address is undefined behavior and generally evil. You are returning the value probably from a stack frame that is no longer valid. Sometimes it might appear to work, but it really isn't and all it is doing is fooling you. – Michael Dorgan Apr 17 '15 at 23:23
  • Yeah. I knew it's not adviced to do so and rarely used in practice. But I am just confused why one works the other doesn't. – Jerry Apr 17 '15 at 23:27
  • My (limited) understanding of c++ is that once you cross into "undefined behavior", the compiler can do anything it wants. So one compiler may let one work, while another compiler may let the other work. – Teepeemm Apr 17 '15 at 23:28
  • yeah. that might be true. I knew it's stupid to do this. – Jerry Apr 17 '15 at 23:33

2 Answers2

4

It's never safe to return a pointer to a local variable. Both of your examples are functionally identical.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
  • I corrected it right now. The two right now are different. Sorry. – Jerry Apr 17 '15 at 23:28
  • 1
    Nit: It's not safe to return a pointer to an *automatic* local variable. It's fine if it's a static or thread-local local variable. – Brian Bi Apr 17 '15 at 23:30
  • The first one really works. I tried in a qt creator ide. It works perfectly and without any warning and error. I am so surprised that it works. – Jerry Apr 17 '15 at 23:30
  • @Jerry, it only looks like it works. – Carl Norum Apr 17 '15 at 23:36
  • @Jerry You don´t seem to understand. a) The compiler won´t give errors, because it assumes you know what you´re doing. b) With `-Wall` or something like that, you probably will get warnings. c) Just because it works now, that doesn´t mean anything. First, you can´t know if there isn´t some problem because of it and you just haven´t noticed it yet. Second, tomorrow or on another computer or in a larger program, it may suddenly not work anymore. – deviantfan Apr 17 '15 at 23:38
2

Both are undefined behaviour. The compiler is free to do whatever it likes in both cases.

The practical reason for the difference is probably that there are two variables rather than one defined in the first version, and the compiler optimiser (or chosen optimisation setting) isn't smart or aggressive enough to optimise it out.

Other than that the two examples are practically identical, in that any possible result is acceptable according to the standard. The result would be equally correct if it reformatted your hard drive in the first case, and shut down your computer in the second.

Peter
  • 35,646
  • 4
  • 32
  • 74