Question about function returning a link in C ++

Is it possible to return a link to a local variable from a function? By local, I mean that the variable will be created (on the stack, that is, without using a new one) inside the function, and its scope is only inside this function. I got conflicting answers when I searched about this. 1) says that the type of use is correct, but 2) contradicts it.

1) http://functionx.com/cpp/examples/returnreference.htm
2) http://www.cprogramming.com/tutorial/references.html (in the "Links and Security" section)

Which of them is right?

Another question that I had is that if 1) is correct, then the following serve the same purpose.

i) int & a = func (); ii) int a = func (); where func () returns a reference to int (a local variable in this function).

In both of these cases, copying the return value does not occur. I would like to prevent copying the return values, since the return value may be large.

Thanks in advance.

Raghava.

+7
c ++ reference
source share
6 answers

As everyone else says, do not do this. Returning a link or pointer to a local variable is always incorrect, because the return act gets rid of the local variable and, therefore, the link or pointer is automatically invalid.

Copying may not be a problem. C ++ compilers are allowed to skip copy constructors when returning from functions ("return value optimization"), so a reasonably intelligent compiler can create a value in place. This way you can return a lot of value without copying. Try and see; you can temporarily output output statements in the copy constructor (if you wrote one and don't use the automatically generated one) to see if it was actually called.

So, without starting and trying, you do not know if the actual copying is taking place, and if so, then what is the problem. As always, time and profile are run to see if there is a problem, and if so, where. Doing something risky and / or confusing to speed up performance is almost never worth doing before synchronization and profiling.

+6
source share

You can return a link to a static local variable in a function. Otherwise, this is a recipe for disaster, since the local variable is destroyed after the function returns.

The following may be useful if you take the same example that you indicated in your first link.

EDIT 2:

For point ii in your post, let's say the function 'fn', as shown

int& fn(){ static int x; return x; } int a = fn(); 

This includes a copy from the Lvalue of the expression 'fn'.

It does not include a copy if it was

 int &a = fn(); 
+6
source share

If you mean a variable internal to the function, no ... it is always pointless to return a link to it. Returning a member variable reference from a member function is excellent.

As for the desire to avoid copying large data structures. Wait until you have actual profile data showing that you are a) actually making a copy, and b) that this is actually a bottleneck. "Premature optimization is the root of all evil," may be a little extreme, but premature optimization certainly causes a lot of pain and trouble, while rarely, if ever, gives ANY of the benefits these attempts are designed to achieve .

+5
source share

The examples that you see in your first link are completely fictitious. This page on functionx.com is trash. Returning a reference to a local (automatic) variable is always an error, since any attempts to access the returned value will lead to undefined behavior.

The second link is correct.

+4
source share

You cannot return a reference to a non-stationary local variable from a function. The variable goes out of scope when the function returns, and therefore the reference will refer to the destroyed object.

Although this may seem to “work” in the first example presented in 1), this bad practice will quickly start to crash you with classes that have a destructor or more.

It is best to return a pointer, return a link to a locally static object, return a link to a member variable.

In this case, if the object is built inside the return function, you can study the implementation of the semantics of C ++ 0x movement in your large class. This could turn a “large copy” into several pointer swaps.

+2
source share

assuming func returns a reference to the value that lives after the function returns (static local or global), then your

 i) int& a = func(); ii) int a = func(); where func() returns a reference to an int (local variable in that function). 

quite different

in (i) is still static or global. Chaning a will change the source variable

in (ii) a is a local copy; changing it does not affect the original variable

0
source share

All Articles