assignment of vectors

S

slurper

if i have a vector and assign another vector to a vector variable like this:

vector<int> c, k;

c.push_back(1), c.push_back(2);
k.push_back(3), k.push_back(4);

c = k;

does this work?? if it does, i assume a copy of k is made and assigned to c.
will the memory occupied by the original c-vector be reclaimed?

tx
 
V

Victor Bazarov

slurper said:
if i have a vector and assign another vector to a vector variable like
this:

vector<int> c, k;

c.push_back(1), c.push_back(2);
k.push_back(3), k.push_back(4);

c = k;

does this work??

Of course it does.
if it does, i assume a copy of k is made and assigned to c.

A copy isn't made, most likely. Every element of 'c' gets the value the
same as the corresponding element of 'k'.
will the memory occupied by the original c-vector be reclaimed?

I am not sure what's "reclaimed" here. Since both vectors have size 2 by
the time you assign k to c, there is likely no [re-]allocation involved.
So, the answer is probably "no", if by "reclaimed" you meant what I think
you meant.

V
 
J

Jive

slurper said:
if i have a vector and assign another vector to a vector variable like this:

vector<int> c, k;

c.push_back(1), c.push_back(2);
k.push_back(3), k.push_back(4);

c = k;

does this work?? if it does, i assume a copy of k is made and assigned to c.
will the memory occupied by the original c-vector be reclaimed?

tx

Yep, yep. After the assignment, c contains [3,4], and there is no
memory leak.

One thing to be aware of is that C++ has no concept of "shallow" or "deep"
copy. A copy does whatever the copy constructor says it does. Default copy
constructors do the right thing: They call the copy constructor of each
sub-object. (In the very early days, that was not the case.) A properly
designed container class has a copy constructor that does the right thing by
the values it contains. If you have a vector of lists, every list gets
copied. If you have a vector of pointers (references) to lists, the
pointers get copied. It's all as it should be, and should be, and should
be, recursively.
 
S

slurper

Jive said:
slurper said:
if i have a vector and assign another vector to a vector variable like this:

vector<int> c, k;

c.push_back(1), c.push_back(2);
k.push_back(3), k.push_back(4);

c = k;

does this work?? if it does, i assume a copy of k is made and assigned to c.
will the memory occupied by the original c-vector be reclaimed?

tx

Yep, yep. After the assignment, c contains [3,4], and there is no
memory leak.

One thing to be aware of is that C++ has no concept of "shallow" or "deep"
copy. A copy does whatever the copy constructor says it does.

c = k ->but this is the assignment operator, no? by assigning k to c, i get
copies of the elements in k into c, right? in other words: c and k won't be
the same vector, but identical copies. when is the copy constructor
involved here?
 
S

slurper

Victor said:
Of course it does.


A copy isn't made, most likely. Every element of 'c' gets the value the
same as the corresponding element of 'k'.

aren't they copied than by definition??? i mean: they both get their own
values, which happen to be the same.
will the memory occupied by the original c-vector be reclaimed?

I am not sure what's "reclaimed" here. Since both vectors have size 2 by
the time you assign k to c, there is likely no [re-]allocation involved.
So, the answer is probably "no", if by "reclaimed" you meant what I think
you meant.

if i understand it all now: c=k means c ends up with the same values of k,
and the old values of c will be overwritten by the new values

so if i do
vector<int> c, k;

c.push_back(1), c.push_back(2), c.push_back(5);
k.push_back(3), k.push_back(4);

c = k;

k will be (3,4)
c will be (3,4) or (3,4,5) ??
 
V

Victor Bazarov

slurper said:
aren't they copied than by definition??? i mean: they both get their own
values, which happen to be the same.

"A copy of k is made and...". I said that a copy of k is not made. The
values of elements of k are assigned to elements of c and the size of c
is reset to be the same as k (or, rather, the size first, then copying of
the elements).
will the memory occupied by the original c-vector be reclaimed?

I am not sure what's "reclaimed" here. Since both vectors have size 2 by
the time you assign k to c, there is likely no [re-]allocation involved.
So, the answer is probably "no", if by "reclaimed" you meant what I think
you meant.

if i understand it all now: c=k means c ends up with the same values of k,
and the old values of c will be overwritten by the new values

so if i do
vector<int> c, k;

c.push_back(1), c.push_back(2), c.push_back(5);
k.push_back(3), k.push_back(4);

c = k;

k will be (3,4)
c will be (3,4) or (3,4,5) ??

If you "understand it all now", why the question? Think of a vector as
a complete object. The _size_ is just as much part of the value as the
storage itself. So, of course c will be (3,4). It can retain its capacity,
but it will have the size the same as k.

V
 
J

Jonathan Mcdougall

if i have a vector and assign another vector to a vector variable like this:
vector<int> c, k;

c.push_back(1), c.push_back(2);
k.push_back(3), k.push_back(4);

c = k;

does this work??

Depends on what you want.
if it does, i assume a copy of k is made and assigned to c.

This is not a "copy" but an assignment. In particular, the assigment
operator is called for the object c. That's different from the copy
constructor.

std::vector<int> c = k;
std::vector<int> c(k);

These two both use the copy constructor and

std::vector<int> c;
c = k;

this one uses the assignment operator. Basically, both the copy ctor
and the assignement operator do the same thing in the sense that they
copy the elements in k, except that the copy ctor works on a not yet
constructed object and the assignment operator is called on an already
created object. That means the assignment operator must make sure
memory (in particular) is correctly managed.
will the memory occupied by the original c-vector be reclaimed?

(This is implementation-defined. It depends on the library you are
using. My answer is based on a common implementation.)

It depends. Vectors keep, among others, two very important variables :

1. the total amount of memory allocated
2. the total amount of memory used

Vectors usually allocate more memory than they need and then keep some
info to know how many elements there are. That means if you do

c = k;

if 'k' uses more memory than 'c' has allocated (for example, k has 25
elements, but c only has memory for 10), 'c' will need to allocate some
more. That means

1. allocating enough to store k's elements
2. copying k's elements
3. deleting the old memory

So in that case, yes, the memory will be deleted ("reclaimed"). If 'c'
has enough memory allocated to accomodate all of k's elements, it does
not need more and therefore doesn't touch the buffer.


The most important thing to remember here is that you (usually) don't
need (or want) to know the internals of the standard library. Get a
good book describing the behaviors of the library (such as Josuttis') so
you know how to use its features, but refrain from wanting to learn
_how_ something works. First, that's not relevant and second, it
probably won't work the same on another compiler.


Jonathan
 
S

slurper

[knip]
"A copy of k is made and...". I said that a copy of k is not made. The
values of elements of k are assigned to elements of c and the size of c
is reset to be the same as k (or, rather, the size first, then copying of
the elements).
will the memory occupied by the original c-vector be reclaimed?

I am not sure what's "reclaimed" here. Since both vectors have size 2
by the time you assign k to c, there is likely no [re-]allocation
involved. So, the answer is probably "no", if by "reclaimed" you meant
what I think you meant.

if i understand it all now: c=k means c ends up with the same values of
k, and the old values of c will be overwritten by the new values

so if i do
vector<int> c, k;

c.push_back(1), c.push_back(2), c.push_back(5);
k.push_back(3), k.push_back(4);

c = k;

k will be (3,4)
c will be (3,4) or (3,4,5) ??

If you "understand it all now", why the question?
just wanted some confirmation: i said: IF i understand it...
don't want to make you angry ;-)
tx for all answers
I just have a difficult time to learn the intricacies of C++
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top