1

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);
}
8
  • There almost is no reason on earth to have a vector of pointers to integers. I can't think of any. Commented Oct 20, 2016 at 14:12
  • One option would be to use a std::vector<std::vector<int>>, to get rid of the dynamically allocated array. Commented Oct 20, 2016 at 14:12
  • It can occasionally be useful as a "row indexer" when modelling a matrix. Commented Oct 20, 2016 at 14:12
  • 1
    A good lesson to learn is that pointers and arrays are different things. Your vector is a vector of pointers, not arrays. Another good thing to know is that arrays are not copyable or assignable. So vectors of arrays are a no-go. Commented Oct 20, 2016 at 14:14
  • Hey guys! Thanks for the suggestion. Please see edit 1. Suppose I use vector instead of pointers, can I access them AFTER the function returns? Commented Oct 20, 2016 at 14:16

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

8 Comments

Ah right. What I meant was if I can still access the "VALUE" of the child vector. So no, I don't need to access the "child". Just its value.
Wait, what? The vector is copied into parentV. You can access it in the callie.
@0x56794E Yes, the values are copied into parentV and you can access that vector.
@NathanOliver Yes, but not through child, OP seemed to imply that.
@NathanOliver thanks for the edit! Yes. I only wanted to know if the integers that got inserted into the child array can still be accessed in foo.
|
1

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.