Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

question on function returning a reference in C++

Tags:

c++

reference

Is it OK to return a reference to a local variable from a function? By local I mean that the variable would be created(on the stack i.e. without using new) within the function and its scope is within that function only. I got contradictory answers when I searched about this. 1) says that kind of usage is correct but 2) contradicts it.

1) http://functionx.com/cpp/examples/returnreference.htm
2) http://www.cprogramming.com/tutorial/references.html (under References & Safety section)

Which of them is right?

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

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

In both the above cases, there is no copying of return value involved isn't it. I would like to prevent copying of return values since the return value could be large.

Thank you in advance.

Raghava.

like image 955
Raghava Avatar asked Sep 01 '10 17:09

Raghava


People also ask

Can you return by reference in C?

Master C and Embedded C Programming- Learn as you go So it is not legal to return a reference to local var. But you can always return a reference on a static variable.

What does return a reference mean?

It means you return by reference, which is, at least in this case, probably not desired. It basically means the returned value is an alias to whatever you returned from the function. Unless it's a persistent object it's illegal.

What is the advantage of returning a reference from the function?

The major advantage of return by address over return by reference is that we can have the function return nullptr if there is no valid object to return.

Which function returns a reference to array?

The function 'fetchrow_arrayref()' returns a reference to an array of row values.


2 Answers

As everybody else is saying, don't do that. Returning a reference or pointer to a local variable is always wrong, because the act of returning gets rid of the local variable and hence the reference or pointer is automatically invalid.

Copying may not be an issue. C++ compilers are allowed to skip copy constructors when returning from functions (the "return value optimization"), so a sufficiently smart compiler might be able to build the value in place. Therefore, you may well be able to return a large value without any copying. Try it and see; you can temporarily put output statements in the copy constructor (if you've written one and aren't using the automatically generated one) to see if it's actually called.

So, without running and trying, you don't know whether there is actual copying going on, and if so how much of a problem it is. As always, time and profile a run to see if there is a problem and, if so, where. Doing anything risky and/or confusing to speed up performance is almost never worth doing before timing and profiling.

like image 137
David Thornley Avatar answered Sep 19 '22 12:09

David Thornley


You can return a reference to a static local variable in a function. Otherwise, it is a recipe for disaster because the local variable is destroyed once the function returns.

Following may be helpful which takes the same example you cited in your first reference.

EDIT 2:

For point ii in your post, let us assume a function 'fn' as shown

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

int a = fn();

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

It does not involve a copy, if it was

int &a = fn();
like image 45
Chubsdad Avatar answered Sep 22 '22 12:09

Chubsdad