12

Lets say I have an object that I dynamically allocated. If I push it into a STL vector, will a reference be inserted into the vector or a copy of this object?

It's a general question. For example:

class vec {
vector<obj> vec;
void addToVec(obj a) {
    // insert a into vec
 }
  ...
  ...
  ...
 }

obj* a = new obj;
 vec* v = new vec;
 vec.addToVec(a); 

If I delete v, will object a die as well?

iammilind
  • 68,093
  • 33
  • 169
  • 336
Shai
  • 1,093
  • 4
  • 13
  • 20
  • 1
    Please show the code you are thinking of :-) – Martin Kristiansen Dec 17 '11 at 14:18
  • 4
    The preachy side of me is coming out and I feel I must tell you that you very likely do not mean the STL but rather the C++ standard library. – Corbin Dec 17 '11 at 14:20
  • 1
    The C++ standard library has adopted the STL for it's containers and algorithms. So, technically, a STL vector is the same as a C++ library vector. – Emile Cormier Dec 17 '11 at 14:23
  • 1
    possible duplicate of [When adding an element to a vector, how to know that a copy of the object is going to be made?](http://stackoverflow.com/questions/8543854/when-adding-an-element-to-a-vector-how-to-know-that-a-copy-of-the-object-is-goi) – Cody Gray - on strike Dec 17 '11 at 14:23
  • 1
    @EmileCormier, it doesn't matter, if it's `Object` then it will be copy of `Object` and if it's `Object*`, then it will be copy of the pointer `Object*` (shallow copy in other words). – iammilind Dec 17 '11 at 14:27
  • Whether it's the same or not, it very unlikely belongs in a library called a STL implementation. The STL naming needs to die off already. They are not equivalent, and STL implementations are rarely ever used anymore. – Corbin Dec 17 '11 at 14:27
  • 3
    Man, I used to care about the difference between the STL and the C++ standard library, but then I joined Stack Overflow and my stack started overflowing with nitpicky comments about the difference, and I suddenly just stopped caring altogether. Unless the difference is relevant to your question (and it's not here), I don't understand how it possibly matters. – Cody Gray - on strike Dec 17 '11 at 14:28
  • This is what i figured. The code above will not work, you will be trying to give a obj pointer as an argument to a function that takes a obj... but other than that -- adding a obj to that vector would result in a copy. – Martin Kristiansen Dec 17 '11 at 14:29
  • It doesn't matter. The question asker might ask something involving it later in a context where it does matter though. I hear it called the wrong thing all the time, and it annoys me. I'm just, as you said, nitpicking. – Corbin Dec 17 '11 at 14:30
  • Just think of the *S* in STL as an homage to Stepanov. :-) His ideas on generic programming were monumental. It's a shame that people want to ignore his great legacy. I salute you, Mr. Stepanov! :-) – Emile Cormier Dec 17 '11 at 14:36

3 Answers3

9

will a reference be inserted into the vector or a copy of this object?

Copy (which means that your class should be copy-able otherwise compiler error).

Clarification: References cannot be assigned in std::vector<>. Also, here object has broader sense, it can be a normal variable or a pointer, but std::vector<> accepts only copy.

Update: Post C++11, most of the standard containers offer std::move() of the object using "rvalue based API methods"; where a copy may not be performed.

SridharKritha
  • 8,481
  • 2
  • 52
  • 43
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • I was surprised, I thought the compiler would optimize away the copy operation in some cases, but found it didn't: http://ideone.com/b4fpc even with g++ -O6 – matiu Dec 17 '11 at 14:40
  • This one demonstrates how expensive the copy operation can be by copying a 131 KB object: http://ideone.com/UNKyr – matiu Dec 17 '11 at 14:58
9

If you have an object of type T that you have dynamically allocated and you push a pointer to the object onto a std::vector<T*>, then a copy of the pointer is pushed. If you dereference the pointer and push the result onto a std::vector<T>, then a copy of the object is made. Collections always make copies. So collections of pointers make copies of the pointer and collections of class instances make copies of the instances themselves (using copy construction IIRC).

D.Shawley
  • 58,213
  • 10
  • 98
  • 113
3

Have you checked the reference:

void push_back ( const T& x );

Add element at the end

Adds a new element at the end of the vector, after its current last element. The content of this new element is initialized to a copy of x.

This effectively increases the vector size by one, which causes a reallocation of the internal allocated storage if the vector size was equal to the vector capacity before the call. Reallocations invalidate all previously obtained iterators, references and pointers

Alok Save
  • 202,538
  • 53
  • 430
  • 533