0x56794E
0x56794E

Reputation: 21291

C++ vector: push_back an array of int => Does array get copied?

I'm new to C++'s object lifetime, so bear with me.

I have a vector of dynamically allocated arrays of integers: std::vector<int*>

This page says "The content of val is copied (or moved) to the new element."

What I understand from this is that what I push into the array MAY be moved or copied, but when is it moved and when is it copied?

I suspect the valued is copied if it's of primitive types? E.g., int, char, etc?

And it's copied otherwise? Does that means my array would be "moved"?

===================

EDIT 1: what I'm trying to find out is that suppose I pass the vector into a function. In this function I allocate an array of integers and push it into the vector. Once the function returns and I'm back to the caller, can I still safely access the array that was just pushed into the vector?

EDIT 2: some suggested using vector<vector<int>>, so my question became, if I pass the "parent" vector into some function. In this function, I create the inner vector and push it into the outer vector. When I'm back to the caller, can I still safely access the new inner vector that was just pushed into the outer vector? Something like this:

void foo()
{
    vector<vector<int>> parentV;
    addVect(parentV);

    //Is is safe to access parentV[0][0] here?
}


void addVect(vector<vector<int>> &parentV)
{
    vector<int> child;
    child.push_back(1);
    child.push_back(2);
    parentV.push_back(child);
}

Upvotes: 1

Views: 546

Answers (3)

Slava
Slava

Reputation: 44258

but when is it moved and when is it copied?

It is based on value type that you pass. For example std::vvector::push_back() has 2 overrides:

void push_back( const T& value );   
void push_back( T&& value );

so if you pass a type that can bind to rvalue refence, then second method is called and object is moved. Otherwise first method is called and object is copied.

I suspect the valued is copied if it's of primitive types? E.g., int, char, etc?

No it is based on value type, not if type primitive etc. For example:

 std::vector<Foobar> vec;
 Foobar f;
 vec.push_back( Foobar() ); // temporary binds to rvalue, move
 vec.push_back( f ); // value copied
 vec.push_back( std::move( f ) ); // now f moved

Does that means my array would be "moved"?

No your array cannot be moved. Vector could only move a variable that passed to it. Your variable is a pointer, not array. For raw pointer move is the same as copying one pointer to another.

Upvotes: 0

Hatted Rooster
Hatted Rooster

Reputation: 36483

some suggested using vector>, so my question became, if I pass the "parent" vector into some function. In this function, I create the inner vector and push it into the outer vector. When I'm back to the caller, can I still safely access the new inner vector that was just pushed into the outer vector?

void foo()
{
    vector<vector<int>> parentV;
    addVect(parentV);

    //Is the "child" vector still safe for access here?
}


void addVect(vector<vector<int>> &parentV)
{
    vector<int> child;
    child.push_back(1);
    child.push_back(2);
    parentV.push_back(child);
}

To answer:

Is the "child" vector still safe for access here?

No and yes.

No, not through the name child at least. child is local to addVect and thus foo won't know anything about it. It will be destroyed after addVect returns.

Yes, you can access it's values through parentV since they've been copied to parentV and you're passing parentV as reference.

auto copyOfChild = *parentV.rbegin(); //get the last vector since push_back adds to the end of the vector

or

auto copyOfChild = parentV[parentV.size() - 1]; //get the last vector since push_back adds to the end of the vector

Upvotes: 1

xaxxon
xaxxon

Reputation: 19761

The pointer stored in the vector may be moved, but the address it points to won't, which means your data never move. If you had the actual ints in the vector and then took an address to one of them, that pointer could be invalidated if the vector had to be re-allocated, but since only the pointer is stored in the vector, your actual ints in the arrays will never be relocated.

Upvotes: 1

Related Questions