473,670 Members | 2,307 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1773
"slurper" <sl*********@sk ynet.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*********@sk ynet.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*********@sk ynet.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*********@sk ynet.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*********@sk ynet.be> wrote...
Victor Bazarov wrote:
"slurper" <sl*********@sk ynet.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
3412
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 objects in the vector vec_A. Suppose there are K attributes to be added. For each of the attributes I define K vectors of appropriate types. Say, the attributes have types type1, type2, ..., typeK. So I define std::vector<type1> attr1(vec_A.size());
5
2312
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 seem to be (in my eyes at least) are glorified Arrays. - Now I know there's a bit more difference, but what exactly are the advantages of Arrays over Vectors (if any)? Oh, and please keep it in mind I am a real beginner in C++ but can
3
3239
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 there anyway where I can accomodate both the vector types into a single set. Something like a set<vector<void*>, my_compare func >. Right now, I am having them as two different set dayatypes.
4
2013
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
2767
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 in a nested scope, on variables from the outer scope: PythonWin 2.4.3 (#69, Mar 29 2006, 17:35:34) on win32. Portions Copyright 1994-2004 Mark Hammond (mhammond@skippinet.com.au) -
5
18135
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 " << v1.size( ) << endl; v1.clear( ); //clears the vector I have a few questions:
1
6235
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 library from the higher level classes. This low level class is used to allocate locked pages of memory before doing a DMA. The third party library locks memory and returns a pointer to an internal structure, which we can simply cast to a (void...
1
2059
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 function that handles vectors of vectors of <typename T(which could be anything from int to vectors of something else). As well, I have specialized/overloaded functions to handle the single-level vectors of data (e.g. vector<string>). So the templated...
0
8469
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8903
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8814
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7419
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5684
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2042
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1794
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.