# Passing a vector... by reference?

This topic is 4969 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I want to pass a vector into a function, and I currently have as a parameter... vector<Tile*> in_selected I assume vector is a class, so would this pass in the entire class on the stack? Would I need to use... vector<Tile*> &in_selected To pass the vector in by reference? Thanks

##### Share on other sites
Yes, the former would create a copy on the stack. And yes, the latter would pass by reference.

##### Share on other sites
Quote:
 Original post by CatafriggmYes, the former would create a copy on the stack. And yes, the latter would pass by reference.

Cool. First time I have used vectors, so I was unsure how they behaved.

Thank you!

##### Share on other sites
If the contents of the vector aren't going to be changed you can pass it by constant reference. This communicates to the user that you promise not to change it. It would mean that if someone had a constant vector they could use your function. If you don't change it they won't be able to use it.

void SomeFunction(const std::vector<Tile*>& in_selected) {    //can use vector but can't modify it}

##### Share on other sites
Quote:
 Original post by Raeldor... First time I have used vectors ...

Then you also probably don't want to store pointers but the actual type, unless you have good specific reason to do so prefer storing the actual type over storing pointers. Obviously this is not correct in all contexts but in general prefer it where applicable:

typedef std::vector<Tile> tile_vec;void foo(const tile_vec& v) { /* ... */ }//...tile_vec t;t.push_back(tile(...));t.push_back(tile(...));foo(t);

##### Share on other sites
Quote:
Original post by snk_kid
Quote:
 Original post by Raeldor... First time I have used vectors ...

Then you also probably don't want to store pointers but the actual type, unless you have good specific reason to do so prefer storing the actual type over storing pointers. Obviously this is not correct in all contexts but in general prefer it where applicable:

typedef std::vector<Tile> tile_vec;void foo(const tile_vec& v) { /* ... */ }//...tile_vec t;t.push_back(tile(...));t.push_back(tile(...));foo(t);

amen! i can't believe the over use of the heap i've seen on these forums. take advantage of C++'s scope-based deterministic destruction. if you must allocate memory using new or because you're using a lib that hands you object that way i would suggest using the boost.smart_ptr lib.

just my 2c,
_ulgy

##### Share on other sites
Quote:
 Original post by mgarrissamen! i can't believe the over use of the heap i've seen on these forums. take advantage of C++'s scope-based deterministic destruction. if you must allocate memory using new or because you're using a lib that hands you object that way i would suggest using the boost.smart_ptr lib.

My understanding of the standard library (correct me if i'm wrong - this is from Effective STL item 3), is that when you push_back an object onto a vector, you're not actually putting the object in the vector, rather you're putting a copy of the object into it. And when you get an object back from the container, its a copy of the object. And once inside a container, it's not uncommon for it to be copied further. Meaning you might need a copy constructor and copy assignment operator. Using pointers to objects in containers makes the copying cheap and correct (at the expense of having to use the heap).

Memory deallocation is a pain though, it's not that difficult to .clear() a vector forgetting to delete the objects first.

##### Share on other sites
Quote:
Original post by Raeldor
Quote:
 Original post by CatafriggmYes, the former would create a copy on the stack. And yes, the latter would pass by reference.

Cool. First time I have used vectors, so I was unsure how they behaved.

That is the way it works for all parameters, not just vectors.

##### Share on other sites
Are the contents of a vector copied as well...?

##### Share on other sites
Quote:
Original post by JohnBolton
Quote:
Original post by Raeldor
Quote:
 Original post by CatafriggmYes, the former would create a copy on the stack. And yes, the latter would pass by reference.

Cool. First time I have used vectors, so I was unsure how they behaved.

That is the way it works for all parameters, not just vectors.

To be fair, it is possible that vectors could be reference counted and would do about the same thing either way.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 17
• 14
• 10
• 9
• 11
• ### Forum Statistics

• Total Topics
634097
• Total Posts
3015505
×