473,383 Members | 1,803 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

assignment of vectors

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

Jul 22 '05 #1
8 1758
"slurper" <sl*********@skynet.be> wrote...
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
Jul 22 '05 #2

"slurper" <sl*********@skynet.be> wrote in message
news:41***********************@news.skynet.be...
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.

Jul 22 '05 #3
Jive wrote:

"slurper" <sl*********@skynet.be> wrote in message
news:41***********************@news.skynet.be...
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?
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.


Jul 22 '05 #4
Victor Bazarov wrote:
"slurper" <sl*********@skynet.be> wrote...
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'.


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


Jul 22 '05 #5
"slurper" <sl*********@skynet.be> wrote...
Victor Bazarov wrote:
"slurper" <sl*********@skynet.be> wrote...
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'.


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
Jul 22 '05 #6
> 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
Jul 22 '05 #7
[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++
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


Jul 22 '05 #8
Busted! Of course I meant assignment operator.
Jul 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
5
by: Computer Whizz | last post by:
I was reading through Accelerated C++ at work when I read through the first mention of Vectors, giving us certain functions etc. Is there any benefit of Arrays over Vectors? Since all Vectors...
3
by: Amit | last post by:
Hello. I am having some problem organizing a set of vectors. The vectors itself, could contain a pointer( say integer pointer) or could contain another object MyClass. 1>So, first of all, is...
4
by: Dr. J.K. Becker | last post by:
Hi all, I have vectors that holds pointers to other vectors, like so: vector<whatever> x; vector<whatever*> z; z=&x; Now I add something to x
37
by: Tim N. van der Leeuw | last post by:
Hi, The following might be documented somewhere, but it hit me unexpectedly and I couldn't exactly find this in the manual either. Problem is, that I cannot use augmented assignment operators...
5
by: madhu | last post by:
http://msdn2.microsoft.com/en-us/library/fs5a18ce(VS.80).aspx vector <intv1; v1.push_back( 10 ); //adds 10 to the tail v1.push_back( 20 ); //adds 20 to the tail cout << "The size of v1 is " <<...
1
by: David Bilsby | last post by:
All Apologies for cross posing this but I am not sure if this is a VC 8 STL bug or simply an invalid use of the iterator. I have a PCI card access class which basically abstracts a third party...
1
by: Rob | last post by:
How would I do this? I want to be able to handle vectors of many different types of data and vectors that can contain any number of other vectors of data. Currently, I have a templated...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.