errno_44
errno_44

Reputation: 151

What happens to the memory in a vector when the vector is re-assigned

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;
    }
}

Upvotes: 1

Views: 412

Answers (2)

JaMiT
JaMiT

Reputation: 16963

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.

Upvotes: 1

gct
gct

Reputation: 14583

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.

Upvotes: 3

Related Questions