return a reference

E

emannion

I am trying to clear up some reference questions

if I have a function that returns a reference to an int will that data
be lost when the function goes out of scope


int& retRef()
{
int j=10;

return j;
}

int main()
{

int& k = retRef();

int l = k;

return 0;
}

k is set to 10 after retRef() is called from main, I thought this
would not be possible if retRef() goes out of scope.

Can anyone answer this.

em
 
V

Victor Bazarov

I am trying to clear up some reference questions

if I have a function that returns a reference to an int will that data
be lost when the function goes out of scope


int& retRef()
{
int j=10;

return j;
}

int main()
{

int& k = retRef();

int l = k;

return 0;
}

k is set to 10 after retRef() is called from main, I thought this
would not be possible if retRef() goes out of scope.

Any use of the returned reference has *undefined behaviour*, which
includes having what appears to be the actual intended effect. But
it is *undefined* nonetheless, IOW it may or may not even work the
next time run the same program on the same computer.

V
 
A

Alf P. Steinbach

* (e-mail address removed):
I am trying to clear up some reference questions

if I have a function that returns a reference to an int will that data
be lost when the function goes out of scope


int& retRef()
{
int j=10;

return j;
}

Undefined behavior where the reference is used by the caller.


int main()
{

int& k = retRef();

int l = k;

return 0;
}

k is set to 10 after retRef() is called from main, I thought this
would not be possible if retRef() goes out of scope.

Undefined behavior includes that the program may produce what you
erronously expect.

In practical terms, for a typical C++ implementation you have a
reference to a location on the stack, and that location has not been
reused (it could be reused by a simple expression evaluation).

Cheers, & hth.,

- Alf
 
T

Tomás Ó hÉilidhe

emannion:
if I have a function that returns a reference to an int will that data
be lost when the function goes out of scope


Off hand, I can only think of four instances in which you'd return a
reference from a function:

1: int &Func() { return my_global_var; }

2: int &Func() { static int i; return i; }

3: int &Func() { return *new int; }

4: int &Func(SomeStruct ss) { return ss.i; }


Case 1 deals with global objects
Case 2 deals with static objects within functions
Case 3 deals with dynamically allocated objects
Case 4 deals with getting an object from the argument somehow

They're the "natural" cases that come to mind. Maybe if I thought about
it hard enough for a while, I could come up with another reason for
returning a reference from a function, but none come to mind instantly.
 
A

Andrey Tarasevich

Tomás Ó hÉilidhe said:
...
Off hand, I can only think of four instances in which you'd return a
reference from a function:
...
Case 1 deals with global objects
Case 2 deals with static objects within functions
Case 3 deals with dynamically allocated objects
Case 4 deals with getting an object from the argument somehow
...

You forgot the case when it returns a reference to a data member of its
own object. But anyway, trying to enumerate these cases in such a
specific way is a waste of time. The important thing is they all have
the same common property: the object being referenced continues to exist
after the function exits.
 
A

acehreli

you mean
4: int &Func(SomeStruct& ss) { return ss.i; }
of course.

No, no! Tomas is correct; because SomeStruct contains a reference to
an int, and has a trivial copy constructor! ;)

struct SomeStruct
{
int & i;

SomeStruct(int & i_)
:
i(i_)
{}
};

int &Func(SomeStruct ss) { return ss.i; }

#include <iostream>

int main()
{
int i = 42;

SomeStruct ss(i);
int & j = Func(ss);

std::cout << j << '\n';
}

The behavior is well defined. :)

Ali
 
A

acehreli

On Nov 30, 1:42 pm, (e-mail address removed) wrote:
[...]
std::cout << j << '\n';

}

The behavior is well defined. :)

Actually no! If I'm not mistaken, the standard doesn't define whether
j will be flushed to cout without flushing std::cout (explicitly with
std::flush or possibly implicitly through std::endl). I love C++! :)

Ali
 
R

Rolf Magnus

if I have a function that returns a reference to an int will that data
be lost when the function goes out of scope

Not necessarily.
int& retRef()
{
int j=10;

return j;
}

In this case, yes. It's not exactly due to returing a reference, but rather
due to having a reference to an object that is already destroyed. j goes
out of scope as soon as retRef() returns.
int main()
{

int& k = retRef();

At this stage, there is no real error yet. Having a reference to a destroyed
object is ok, but you must not use it, so generally, returing a reference
to a local object makes no sense. However, returing a reference to an
object that lives longer than the function executes would be just fine.
int l = k;

This line invokes undefined behavior by using a reference to an object that
doesn't exist anymore.
return 0;
}

k is set to 10 after retRef() is called from main, I thought this
would not be possible if retRef() goes out of scope.

Well, the behavior is undedfined, which means anything can happen, including
things you don't expect.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,186
Messages
2,570,998
Members
47,587
Latest member
JohnetteTa

Latest Threads

Top