I'm currently undertaking a C++ course at university. I understand the general concept of shallow and deep copying using vectors however there's an example in my textbook that has me confused.
Please assume that it is a poorly implemented vector with no copy constructor defined so that it only performs a shallow copy of the data.
I understand what's happening in the first part
In the statement
vector<int> v2(v1);
vector
v1
is passed as a const reference argument to the vector copy constructor, sov1
can’t be changed, and the variablev2
is then initialized to a copy of the vectorv1
. Each data field will be copied, and any changes made later tov2
should not affectv1
. When the value inv1.the_data
is copied over, bothv1.the_data
andv2.the_data
will point to the same arrayBecause
v1.the_data
andv2.the_data
point to the same object, the statementv1[2] = 10;
also changes
v2[2]
. For this reason,v2
is considered a shallow copy ofv1
.
However I'm struggling to understand this part. I'm not quite sure why v2.num_items
won't also change in a shallow copy.
The statement
v1.push_back(20);
will insert
20
intov1[5]
and will changev1.num_items
to 6, but will not changev2.num_items
.
My current thoughts on it are that v1.the_data
and v2.the_data
are pointing to the same place in memory therefore they 'share' the same vector so that when 20 is added to the end of it both of the vectors should gain an additional integer.
I would greatly appreciate assistance in understanding why the number of items won't change for v2
when v1
is modified.
Assuming we are talking about the standard std::vector
:
When you copy the vector in this statement :
vector<int> v2(v1);
v2 is built by copying each element of v1. v1 and v2 do not share any of their memory.
This part :
both v1.the_data and v2.the_data will point to the same array
Because v1.the_data and v2.the_data point to the same object,
Is wrong.
You can convince yourself by comparing the underlying arrays addresses of each of your vectors with the data()
member function.
EDIT :
Assuming you are crazy enough to not use std::vector
and use an implementation that would "share" its back end array when copied (I wont talk about the issues with this design : who owns the array ? who delete[]
it ?)
The issue raised by your teacher is that when v1
is modified (e.g. an element is added), v2
does not know about it, and has an unchanged size.
Any push_back
(or the likes) made to one vector should be observed by every other owner of the array, to properly reflect the size of the array.
Either :
1) you implement some kind of observer pattern to have each vector aware of any modification (and it is more difficult than it sounds)
2) you use tricks to store the length in the backend array itself.
You would run into similar issues to invalid every iterators when the "shared" array is modified through one of the vectors references... A nightmare ! There are good reasons why the STL containers were all designed to managed their own memory, hence always providing deep copy semantics.