DIR Return Create A Forum - Home
---------------------------------------------------------
techsuns
HTML https://techsuns.createaforum.com
---------------------------------------------------------
*****************************************************
DIR Return to: OutPut of a Program
*****************************************************
#Post#: 37--------------------------------------------------
returning c++ vector from a function...
By: dinesh Date: August 21, 2012, 2:52 am
---------------------------------------------------------
std::vector<int> foo()
{
std::vector<int> v;
/* fill it */
return v; // (1)
}
void bar()
{
std::vector<int> v = foo(); // (2)
v = foo(); // (3)
}
What really happens here?
Mention the steps using the line numbers given (commented)
#Post#: 38--------------------------------------------------
Re: returning c++ vector from a function...
By: ravu Date: August 22, 2012, 1:03 am
---------------------------------------------------------
I think
first step you are trying to return a vector .
Second step- I think copy constructor of Vector is going to be
called and the vector is going to be copied into v which was
declared in bar()
third step should be error,
#Post#: 39--------------------------------------------------
Re: returning c++ vector from a function...
By: dinesh Date: August 22, 2012, 1:38 am
---------------------------------------------------------
please specify when what constructor is called and how many
times...
eg:
In (3) copy constructor is called.
#Post#: 164--------------------------------------------------
Re: returning c++ vector from a function...
By: kpr29 Date: November 19, 2012, 11:00 pm
---------------------------------------------------------
@Majeti: I feel in step 3 Shallow copy constructor is
called......... In step 2 deep copy(Guessing)
kp
#Post#: 179--------------------------------------------------
Re: returning c++ vector from a function...
By: srini Date: November 20, 2012, 10:23 am
---------------------------------------------------------
At (1), copy constructor is called. To make things clear (for
future reference), let me give the situations in which a copy
constructor is called (taken from web)
a. When instantiating one object and initializing it with
values from another object
b.When passing an object by value.
c. When an object is returned from a function by value.
At (2), it is shallow copy,(3) it is deep copy. please do some
research on these.
#Post#: 186--------------------------------------------------
Re: returning c++ vector from a function...
By: kpr29 Date: November 21, 2012, 12:46 am
---------------------------------------------------------
@Srinu: Can u please elaborate why it is shallow at step2 and
deep copy at step 3........
#Post#: 189--------------------------------------------------
Re: returning c++ vector from a function...
By: srini Date: November 23, 2012, 11:35 pm
---------------------------------------------------------
@Kp: To correct my above answer, At (2) and (3) it is deep copy.
The answer depends on the type of objects that we are dealing
with. If it is primitive data type, then both default copy and
assignment operators use shallow copy. If data type is dynamic
then they use deep copy (little doubtful).
At (2), a new object v is created before copying can occur. So a
copy constructor is called. In C++ the default copy constructor
performs deep copy.
At(3), the object v is already created so a default assignment
operator is called for copying. since v is a vector (memory
allocated dynamically) so deep copy is performed (almost like
pass by value).
@dinesh: Am I right?
#Post#: 197--------------------------------------------------
Re: returning c++ vector from a function...
By: kpr29 Date: November 26, 2012, 12:37 am
---------------------------------------------------------
@Srinu: Still not so clear........... It will be better if
Majeti gives the answer....
@Majeti: Waiting for the proper explanation in a simple
words.......
#Post#: 205--------------------------------------------------
Re: returning c++ vector from a function...
By: dinesh Date: November 26, 2012, 8:53 am
---------------------------------------------------------
(2) really is a copy constructor, and not a default constructor
+ assignment. This is mandated by the standard, and that's the
reason why it is used both examples (2) and (3).
So...
(2) call foo
(1) copy constructor
(back to 2) copy constructor again (ouch!)
(3) call foo
(1) copy constructor
(back to 3) assignment operator, which is usually implemented as
a copy constructor (ouch again!) + swap
So as you can see, the problem is that returning an object will
trigger a copy.
Most modern compilers have an optimization called "Return Value
Optimization" (RVO for short) which is most of the time able to
merge the steps (1) and (back to...).
Consider:
void foo(std::vector<int>& retVal)
{
std::vector<int> v;
/* fill it */
retVal.swap(v); // (1)
}
void bar()
{
std::vector<int> v; // (2)
foo(v); // (3)
}
Now we're left with:
(2) default constructor, quite lightweight to say the least
(3) call foo
(1) swapping both vectors, again hyper lightweight
(back to 3) nothing more
#Post#: 215--------------------------------------------------
Re: returning c++ vector from a function...
By: kpr29 Date: November 27, 2012, 12:25 am
---------------------------------------------------------
@Majeti: Very Good......... I saw this in their
website.......... Can u give explanation in simple words on
whatever u understood
*****************************************************