-2

I would like to ask the community about std::vector, coming from C.

std::vector<int> temp1(4,0);
temp1[0] = 10;
std::vector<int> temp2;
temp2 = temp1;
temp1[0] = 0;
for (int i=0;i<4;i++)
    std::cout<<"temp1: " <<temp1[i]<<"temp2: "<<temp2[i]<< std::endl;

temp1: 0 temp2: 10

temp1: 0 temp2: 0

temp1: 0 temp2: 0

temp1: 0 temp2: 0

at temp2 = temp1, temp2 appears to be a deep-copy, is this correct? Also, what happens in this case?

std::vector<int> temp1(4,0);
std::vector<int> temp2(4,1);
for (int i=0;i<4;i++) {
    // some temp2 values filled
    temp1 = temp2;
}
return 0;

when temp1 is updated, what actually happens. Is a copy made and the previous temp1's destructor called?

6
  • Destructors are only called when an object's lifetime ends, e.g. when a local variable with automatic storage goes out of scope.
    – Kerrek SB
    Commented Jul 29, 2015 at 19:23
  • 6
    You know you can look this up pretty easily: std::vector::operator= Commented Jul 29, 2015 at 19:24
  • std::vector::operator= does a deep copy. Its one of the main reasons why std::vector is so generally useful.
    – Chad
    Commented Jul 29, 2015 at 19:30
  • If you really want to know what's going on when you do business with a vector, define a simple class that has logging (std::cout or so) in every constructor, destructor, copy constructor etc, then place those objects in the vector. Commented Jul 29, 2015 at 19:41
  • No research effort shown. These details are basic and can be found on the web or in your C++ book, trivially. Commented Jul 29, 2015 at 19:47

1 Answer 1

1

Because these objects are of type std::vector<int>, yes, it's a deep copy, but no, no destructor is called when you do temp1 = temp2.

If the type were std::vector<X> where X is a class, then temp1 = temp2 would still be a deep copy and (depending on further details of the two vectors and possibly the implementation of std::vector) it might destroy the old contents of temp1--that is, whatever objects of type X were contained in temp1 before the assigment, their destructors might be called. (The call to std::vector<int>::operator= couldn't call any destructors because int has no destructor.)

If the type were std::vector<*X> (now X can be a class or int or some other primitive or even a pointer type), temp1 = temp2 would not execute a deep copy--at least, I would not (usually) call it a deep copy, since only the pointers are copied and they point to the same objects as the pointers that are still in temp2. (I say "usually" because there are some cases where someone can argue that the pointers themselves are the contents of the data structure and therefore the copy is "deep".) No destructor is called in this case, even if X is a class, because *X does not have a destructor.

2
  • If the type were std::vector<X> where X is a class, then temp1 = temp2 would still be a deep copy , but it would call the class X copy constructor for each object of X contained in the temp1 container and i dont think the destructor for class X would be called unless the temp1 container clear is called or goes out of scope
    – Ram
    Commented Jul 29, 2015 at 19:50
  • @Ram Good point. Cplusplus.com says, "Any elements held in the container before the call are either assigned to or destroyed." C++11 allows for the allocator to be replaced with a different allocator, in which case all contents are destroyed, but otherwise there does not seem to be a requirement to destroy contents. Another question is what happens if temp2.size() is much less than temp1.size(); could the std::vector reallocate to a smaller size to free memory?
    – David K
    Commented Jul 29, 2015 at 21:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.