0

j is destroyed when the function calls return

k is destroyed at the end of the enclosing brackets

If i pass in 9 for j, k is created and will be assigned 81

Returning k will set func1 which is a reference to an integer = k

Returning will immediately terminate the function

My question is, are k and j terminated at the return statement?

If they are func1 should reference nothing...

But i have tried to run this code and it works...

int& func1(int j){
 int k = j*j;
 return(k);
}
Jokaaa
  • 37
  • 4
  • Possible duplicate of [C++ Returning reference to local variable](https://stackoverflow.com/questions/4643713/c-returning-reference-to-local-variable) – Algirdas Preidžius Nov 19 '18 at 13:24
  • 1
    "_But i have tried to run this code and it works..._" "It works" is only one of (many) possible manifestations of undefined behavior. It may even continue to work, up until the point you decide to show your code to a potential client, when it might decide to stop working. – Algirdas Preidžius Nov 19 '18 at 13:26
  • This should also trigger a warning from the compiler depending on your compiler and warning level. – uceumern Nov 19 '18 at 13:50
  • @Algirdas Preidžius OMG you are so smart, can we be friends?? – Jokaaa Nov 19 '18 at 14:31
  • What is the reason for it continuing to work, compiler had a memory lapse? – Jokaaa Nov 19 '18 at 14:33
  • @Jokaaa "_What is the reason for it continuing to work, compiler had a memory lapse?_" Compiler's memory had nothing to do with it, since the code is, typically, compiled once, and ran multiple times. It is stated, in C++ standard, that such code would invoke undefined behavior, hence, compiler can compile such code, in whatever way it needs, at the moment of compilation. – Algirdas Preidžius Nov 19 '18 at 14:37
  • @Algirdas Preidžius What is the reason that it works the first time and not for any other times after that? There must be something happening with the compiler stage – Jokaaa Nov 19 '18 at 14:54
  • @Jokaaa As I already mentioned: short answer: undefined behavior is undefined. There's no point in reasoning about it. Slightly longer answers: [Undefined, unspecified and implementation-defined behavior](https://stackoverflow.com/questions/2397984/undefined-unspecified-and-implementation-defined-behavior), [Undefined behavior](https://en.cppreference.com/w/cpp/language/ub). – Algirdas Preidžius Nov 19 '18 at 15:17

2 Answers2

1

and it works...

No, it appears to work. The moment you try to access the reference returned by func1 you enter the Undefined Behavior realm. At that point all bets are off, it could've printed out 42, printed out nothing, crash, eat your CMOS battery etc.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122
0

If they are func1 should reference nothing...

You are correct. Try using the reference some more time and you will see:

#include <iostream>

int& func1(int j) {
    int k = j * j;
    return(k);
}

int main() {
    int& addr = func1(9);

    for (int i = 0; i < 10; ++i) {
        std::cout << addr << '\n';
    }
}

Output:

81
2758456
2758456
2758456
2758456
2758456
2758456
2758456
2758456
2758456
Stack Danny
  • 7,754
  • 2
  • 26
  • 55