-1

This answer here uses a * instead of a &, so while it is a great read, I am not understanding how it relates to my question.

I am a little confused as to what is going on when I return an std::vector with and without the &.

For example, if my function definition looked like this:

std::vector < foo > & myFunction(...)
{
   //myVector is made
   return myVector;
}

or this:

std::vector < foo > myFunction(...)
{
   //myVector is made
   return myVector;
}

The only difference here is there is the first function is returning with &, and the second one isnt.

My code seems to work in either case, but I am not sure why... what is going on here exactly? Thanks.

Community
  • 1
  • 1
Spacey
  • 2,941
  • 10
  • 47
  • 63
  • @Borgleader I have edited my question. – Spacey May 27 '15 at 14:30
  • @dasblinkenlight I have edited my question. – Spacey May 27 '15 at 14:31
  • 1
    Actually, the link does answer the question. In both instances (using a `&` and using `*`), you are creating a local variable. In both of these scenarios, you are returning a "referrer" to the local variable. Since that local variable will no longer exists, any form of referring to it outside the function is undefined behavior. Whether it is a reference or pointer, doesn't matter. Note that the solution (if you want to call it that) for both scenarios is to return an object. – PaulMcKenzie May 27 '15 at 15:09
  • @PaulMcKenzie Thanks Paul. I think I am getting it. Would this scenario also be relevant if the vector in question was also an already allocated member function of a class? The context here is that I am trying to write a `getVector` member function that returns a private vector of an object. – Spacey May 27 '15 at 15:37
  • If you're returning a vector that is a member of the object, then that is perfectly ok to return a reference to it. As a matter of fact, it may be the correct thing to do, depending on the usage. See here for a discussion of returning an object as opposed to returning a reference: http://stackoverflow.com/questions/30041907/can-i-use-nested-loops-with-vectors-in-cpp – PaulMcKenzie May 27 '15 at 19:15
  • @PaulMcKenzie Thanks, that helped me, and I upvoted your answer. – Spacey May 27 '15 at 22:08

1 Answers1

3

A reference is just an alias for an object. In the first function, you are returning a reference to a local object, which is destroyed at the end of the function. This is bad, as after the call you end up with what's called a dangling reference, i.e. an alias to an object that's not alive anymore.

The second form is fine, as you return a copy of the local object. In general, you usually return references to i) parameters that are passed to your function by reference, ii) static objects defined in your function (thanks @davidhigh for the comment) or iii) non-local objects.

vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • 1
    unless it's a `static` vector ... ? – davidhigh May 27 '15 at 13:57
  • @davidhigh thanks, I just added the most common scenarios. – vsoftco May 27 '15 at 13:58
  • @davidhigh I doubt that it is static, because the comment says that `myVector` is made inside the function. – Sergey Kalinichenko May 27 '15 at 13:59
  • Thanks, so in this case, what would be the proper way in which to return my vector "by reference"? – Spacey May 27 '15 at 14:14
  • @Learnaholic if the vector is local and non-static, you cannot return it by reference, as it doesn't make sense. The object won't be valid anymore. You must return a copy. – vsoftco May 27 '15 at 14:46
  • @Learnaholic Returning a reference to a local variable is undefined behavior, so you can't return `my vector` by reference. – PaulMcKenzie May 27 '15 at 14:50
  • @Learnaholic and your code seems to be working because probably the memory of your local object was not yet reclaimed by the operating system. However, as PaulMcKenzie mentioned, using the reference later is undefined behaviour! Read the answer in the duplicate, it makes a very nice analogy with hotel rooms. – vsoftco May 27 '15 at 14:56