1

What would happen to the vector myv and the memory that it was using if it gets re-assigned to a different vector? More importantly, what happens when you pass a vector as a reference to a function that re-assigns the vector? Is it just a copy type operation, or does the passed in vector reference the right hand side vector? What if that right hand side vector goes out of scope?

TLDR: what does this actually accomplish?

#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <stdlib.h>

std::vector<std::vector<int> > vectors;

void SetVector(std::vector<int> &v){
    v = vectors.at(0);
}

int main()
{
    std::vector<int> ints;
    ints.push_back(1);
    ints.push_back(1);
    ints.push_back(1);

    vectors.push_back(ints);

    std::vector<int> myv;
    myv.push_back(6);
    myv.push_back(6);
    myv.push_back(6);
    myv.push_back(6);

    SetVector(myv);

    for(size_t i = 0; i < myv.size(); i++){
        std::cout << myv[i] << std::endl;
    }
}
errno_44
  • 151
  • 6
  • Why don't you compile and run your example? Then you will see what happens. – Olaf Dietsche Jan 17 '20 at 16:33
  • 2
    @OlafDietsche That advice is dangerous in c++. Its not because you observe a behavior that the behavior is well defined. – François Andrieux Jan 17 '20 at 16:35
  • I'm not sure I really understand the question. But be sure to understand that containers store and own copies of their elements. The statement `vectors.push_back(ints);` copies `ints` to `vectors`. – François Andrieux Jan 17 '20 at 16:36
  • @OlafDietsche What prints out looks fine, but this seems to be one of those suspicious things.. – errno_44 Jan 17 '20 at 16:37
  • It's not really, the answer below is a good one. – Spidey Jan 17 '20 at 16:37
  • So basically you need to break down this code. Transfer by reference means that whatever you do to that vector inside the function will apply to the vector inside the main. Now you just need to focus on how vector implements the assignment operator for an integer. – Spidey Jan 17 '20 at 16:39
  • @errno_44 What do you mean by suspicious? The code looks pretty simple and straight forward to me. – Olaf Dietsche Jan 17 '20 at 16:48
  • @FrançoisAndrieux Be realistic, there's nothing special in the example shown. The only noteworthy are unnecessary #includes. – Olaf Dietsche Jan 17 '20 at 16:53
  • https://wandbox.org/permlink/FAjlcWJV34oddcjQ – Marek R Jan 17 '20 at 17:39
  • @OlafDietsche I agree. But the advice can easily be taken to be more generally applicable then you may have intended. – François Andrieux Jan 17 '20 at 18:08

2 Answers2

3

This will call the assignment operator of the vector you pass into SetVector. It's equivalent to doing myv = vectors.at(0). The vector implementation will handle managing the memory of myv (freeing if needed), before copying the values in vectors.at(0) to it.

gct
  • 14,100
  • 15
  • 68
  • 107
  • If `myv = vectors.at(0)` and `vectors.at(0)` was a vector of object pointers, would these be valid pointers? – errno_44 Jan 17 '20 at 17:13
  • 1
    @errno_44 They'll be the same pointers you put in `vectors.at(0)`. You tell me whether they're valid or not. – user253751 Jan 17 '20 at 17:21
1

What would happen to [...] the memory that [the vector] was using if it gets re-assigned to a different vector?

A vector's memory management is a black box. If you think your program's correctness hinges upon such a question, you probably are asking the wrong question. Focus on the behavior of a vector, not how that behavior is implemented.

What would happen to the vector myv [...] if it gets re-assigned to a different vector?

In this case, myv becomes a copy of that other vector. The length of myv is adjusted (if needed) to match the other vector, and each element of the other vector is copied to the corresponding element in myv.

I should note that it is possible for the assignment operator to move the contents of the other vector to myv instead of copying. However, your sample code indicates copy-assignment rather than move-assignment so I'll stick to assuming copying instead of moving. (Moving is similar to copying with the additional caveat that the object being assigned from is left in an indeterminate, but valid, state. However, that's a separate topic.)

More importantly, what happens when you pass a vector as a reference to a function that re-assigns the vector?

There is nothing special about vectors in this context. If you have a reference to an object, then that reference is equivalent to the variable naming the object (modulo lifetime considerations); assigning a value to that reference is equivalent to assigning a value to the original object. In this case, the original vector becomes a copy of the other vector.

Is it just a copy type operation, or does the passed in vector reference the right hand side vector?

I'm not sure how to parse this. Perhaps you would be re-assured being told that a reference variable cannot be bound to a different object after initialization? See also What are the differences between a pointer variable and a reference variable in C++?

What if that right hand side vector goes out of scope?

None of the information provided indicates that this has any detrimental effect.

JaMiT
  • 14,422
  • 4
  • 15
  • 31