简体   繁体   中英

What happens when you return a reference from a c++ function?

The question is not why and when one should use it. I'm wondering what exactly happens in the inferior level, because we get really curious results doing it.

For example, it is clear when you use it like this:

int& func(int& num) {
    ++num;
    return num;
}

Here we pass a variable by reference, change it, and return a new reference to the same variable.

But what happens if we do this? :

int& func(int num) {
    ++num;
    return num;
}

The variable num ceases to exist when the function func is done. C++ does not allow to create references without initializing them with a variable. But in this case, both compilation and execution go without errors. For example, if we don't initialize an int , we get random behavior:

int num;
std::cout << num; // gives 6422352 or sth like that

But if we initialize it with the function above:

int num1(1);
int num2(func(num1));
cout << num2; // console freezes for a second or two, and execution stops, without any errors

So I've been wondering:

  1. Why are we not getting a compilation error returning a reference to a non existent object?
  2. What is this that is assigned to num2 ?
  3. Why does it not cause any execution errors, but just stops the program?

There is nothing wrong with returning a reference to a local variable from a function. However using that reference results in undefined behaviour.

Well, I compiled your code with g++ version 5.4.0 .

So I've been wondering:

  1. Why are we not getting a compilation error returning a reference to a non existent object?

The compiler doesn't give an error, but it does give you a warning.

sampe.cpp: In function ‘int& func(int)’:
sampe.cpp:6:15: warning: reference to local variable ‘num’ returned [-Wreturn-local-addr]
 int& func(int num)
               ^

Also, you are not returning a reference to a non-existent object. When you are returning the reference, the object (or rather int ) is present and not out-of-scope. Once the return statement is executed, then it goes out-of-scope .

That's why the compiler compiles the code and just gives a warning.

  1. What is this that is assigned to num2?
  1. Why does it not cause any execution errors, but just stops the program?

Now, when I execute the program, I get Segmentation fault , every single time (without any output). A quick use of gdb tells me that the line

int num2(func(num1));

in main() is the culprit. It's where my program crashes.

====================================================

TLDR;

As stated in comments to your question and the answer by @Hi - love SO, it's clear that there's nothing wrong with returning a reference to a local variable.

May be you are just using an old compiler which is somehow able to give you output as your program is entering the Undefined behaviour territory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM