assigning reference to a returned value from function.

Y

ypjofficial

Hello All,

I need to return a vector from a function.The vector is of int and it
can contain as many as 1000 elements.Earlier I was doing
//function definition
vector<int> retIntVector()
{
vector<int> vecInt;
/*
//Populate the vector with int values.
*/
...
....
return vecInt;

}
//end function Definition
int main()
{
vector<int> vect_int = retIntVector();
...
...
return 1;
}
//Program End
But I tempt to use a reference to vector returned by the function to
enhance the performance
like

vector<int> & vect_int =retIntVector();

But somehow I could not convince myself over using this approach as in
this case there is no concrete object to which the vect_int refer.
I want to ask you whether the later approach is correct or not?
I ran it on msvc 6 and it gave me fine results..


Thanks and Regards,
Yogesh Joshi
 
I

Ian Collins

Hello All,

I need to return a vector from a function.The vector is of int and it
can contain as many as 1000 elements.Earlier I was doing
//function definition
vector<int> retIntVector()
{
vector<int> vecInt;
/*
//Populate the vector with int values.
*/
..
...
return vecInt;

}
//end function Definition
int main()
{
vector<int> vect_int = retIntVector();
..
..
return 1;
}
//Program End
But I tempt to use a reference to vector returned by the function to
enhance the performance
like

vector<int> & vect_int =retIntVector();
Where is the vector you reference? If it's a local variable in
retIntVector you are asking for trouble.

Why not to the simple and efficient thing and pass in a reference to the
vector?

void retIntVector( vector<int>& );
 
Y

ypjofficial

Where is the vector you reference? If it's a local variable in
retIntVector you are asking for trouble.
As shown in the code..the vector which is returned by the function is
local to the function.
Why not to the simple and efficient thing and pass in a reference to the
vector?

void retIntVector( vector<int>& );

--
This function is already built and tested and the situation is as good
as i don't have its source code..

Thanks and Regards,
Yogesh Joshi
 
L

Lars Tetzlaff

Hello All,

I need to return a vector from a function.The vector is of int and it
can contain as many as 1000 elements.Earlier I was doing
//function definition
vector<int> retIntVector()
{
vector<int> vecInt;
/*
//Populate the vector with int values.
*/
..
...
return vecInt;

}
//end function Definition
int main()
{
vector<int> vect_int = retIntVector();
..
..
return 1;
}
//Program End
But I tempt to use a reference to vector returned by the function to
enhance the performance
like

vector<int> & vect_int =retIntVector();

But somehow I could not convince myself over using this approach as in
this case there is no concrete object to which the vect_int refer.
I want to ask you whether the later approach is correct or not?
I ran it on msvc 6 and it gave me fine results..


Thanks and Regards,
Yogesh Joshi

Your vect_int will point to garbage!
Maybe you could create a class?

class MyVecInt : public vector<int>
{
public:
MyVecInt()
{
// Populate me with values
}
};


and


main()
{
MyVecInt vec_int;
}
 
F

Frederick Gotham

posted:
But I tempt to use a reference to vector returned by the function to
enhance the performance
like

vector<int> & vect_int =retIntVector();


Firstly, this won't work because you can't bind a non-const reference to an
R-value.

Even if you were to use a const reference, there's still the chance of a
copy. If you wanted to go the "return by value" route, then choose either
of:

vector<int> v = retIntVector();

or:

vector<int> const v = retIntVector();


The solution I'd got for is the following. I'd change the function into a
constructor:

struct VecRetriever {

vector<int> vec;

VecRetriever( /* Whatever args you want */ )
{
vec.pus...
}
};

int main()
{
VecRetriever vecr;

vecr.vec[0] = ...
}
 
F

Frederick Gotham

Frederick Gotham posted:
The solution I'd got for is the following. I'd change the function into
a constructor:


That method allows the function (which has now become a constructor) to
specify whatever arguments it wants to the constructor of the vector (via
the contructor initialisation list), i.e.:

MyClass(int i, double k) : vec(i - k, k *= 3 + i)

If you don't need that, then there's the other alternative of:

void Func(vector<int> &vec)
{
vec.pus...
}

int main()
{
vector<int> vec;

Func(vec);
}

Also, there's the hardcore method:

#include <new>
#include <iostream>
#include <vector>

using std::cout;
using std::vector;

#include <boost/type_traits/aligned_storage.hpp>
#include <boost/type_traits/alignment_of.hpp>

using boost::alignment_of;
using boost::aligned_storage;

vector<int> &Func(void *const p)
{
vector<int> &vec = *::new(p) vector<int>(5);

/* Do more stuff */

return vec;
}

int main()
{
aligned_storage<sizeof(vector<int>),
alignment_of<vector<int> >::value> mem;

vector<int> &vec = Func(&mem);

/* Do some stuff */

vec.~vector();
}
 
M

mlimber

I need to return a vector from a function.The vector is of int and it
can contain as many as 1000 elements.Earlier I was doing
//function definition
vector<int> retIntVector()
{
vector<int> vecInt;
/*
//Populate the vector with int values.
*/
..
...
return vecInt;

}
//end function Definition
int main()
{
vector<int> vect_int = retIntVector();
..
..
return 1;
}
//Program End
But I tempt to use a reference to vector returned by the function to
enhance the performance
like

vector<int> & vect_int =retIntVector();

But somehow I could not convince myself over using this approach as in
this case there is no concrete object to which the vect_int refer.
I want to ask you whether the later approach is correct or not?
I ran it on msvc 6 and it gave me fine results..

As others have noted, returning a reference is generally bad news. You
might consider returning an auto_ptr< vector<int> >, but depending on
how you use it, the syntax could be ugly. You could, as they said, send
in a reference to the vector, but if your compiler supports the return
value optimization (RVO), it will do that for you automatically if
you're smart about things. See this excerpt from _Efficient C++
Programming_ by Lippman that deals with the subject:

http://www.awprofessional.com/articles/printerfriendly.asp?p=25033&rl=1

(Second time today I posted that link. It's a good one!)

Cheers! --M
 
A

Alan Johnson

Hello All,

I need to return a vector from a function.The vector is of int and it
can contain as many as 1000 elements.

You may find the syntax awkward, and there may be some problem that
hasn't occured to me yet, but the following should efficiently "return"
a large vector from a function.

#include <algorithm>

template <typename Container>
class container_mover
{
private:
Container m_c ;

// Don't allow assignment.
container_mover & operator=(const container_mover &) ;
public:
container_mover(Container & c)
{
move_to(c) ;
}

void move_to(Container & c)
{
// It is important to not call std::swap directly
// so that the most specific swap available is used.
using std::swap ;
swap(c, m_c) ;
}
} ;


Then, you'd use it as follows:

#include <vector>

container_mover< std::vector<int> > f()
{
std::vector<int> v(1000, 5) ;
return v ;
}

int main()
{
std::vector<int> v ;
f().move_to(v) ;
}


Other than syntax, the obvious problem is that functions can only
return named objects.
 
V

Victor Bazarov

Alan said:
You may find the syntax awkward, and there may be some problem that
hasn't occured to me yet, but the following should efficiently
"return" a large vector from a function.

#include <algorithm>

template <typename Container>
class container_mover
{
private:
Container m_c ;

// Don't allow assignment.
container_mover & operator=(const container_mover &) ;
public:
container_mover(Container & c)
{
move_to(c) ;
}

void move_to(Container & c)
{
// It is important to not call std::swap directly
// so that the most specific swap available is used.
using std::swap ;
swap(c, m_c) ;
}
} ;


Then, you'd use it as follows:

#include <vector>

container_mover< std::vector<int> > f()
{
std::vector<int> v(1000, 5) ;
return v ;
}

int main()
{
std::vector<int> v ;
f().move_to(v) ;
}


Other than syntax, the obvious problem is that functions can only
return named objects.

There is no need for any custom classes/functions. I believe a simpler
syntax is to use the 'swap' member function:

std::vector<int> f(); //

int main() {
std::vector<int> v;
f().swap(v); // now 'v' contains the returned vector
}

V
 
A

Alan Johnson

Victor said:
There is no need for any custom classes/functions. I believe a simpler
syntax is to use the 'swap' member function:

std::vector<int> f(); //

int main() {
std::vector<int> v;
f().swap(v); // now 'v' contains the returned vector
}

V

That handles getting the returned temporary into a local variable, but
doesn't help the function avoid copying a local variable to a
temporary.

The class I posted, however, does border on absurdity, as it doesn't
provide any advantages over passing an "out" parameter by non-const
reference.
 
B

Bo Persson

Hello All,

I need to return a vector from a function.The vector is of int and
it
can contain as many as 1000 elements.Earlier I was doing
//function definition
vector<int> retIntVector()
{
vector<int> vecInt;
/*
//Populate the vector with int values.
*/
..
...
return vecInt;

}
//end function Definition
int main()
{
vector<int> vect_int = retIntVector();
..
..
return 1;
}
//Program End

Works fine.
But I tempt to use a reference to vector returned by the function to
enhance the performance

And how long do you think it takes to copy 1000 integers?

On a 3 GHz machine, I would guess less than 0.000001 seconds. Is that
too long?


Bo Persson
 
Y

ypjofficial

And how long do you think it takes to copy 1000 integers?

On a 3 GHz machine, I would guess less than 0.000001 seconds. Is that
too long?
Its not about only vector of 1000 int..by saying that I particulary
mean any vector of any datatype with large number of elements in it..

regds,
Yogesh Joshi
 
Y

yogpjosh

The confusion is because of

vector<int> * fun();
Now this statement will put the constraint on the user either to
collect the return value in vector<int> * variable or leave it..
but
vector<int> fun();
Now the user can collect it in vector<int> or const vector<int> &.

Regards,
Yogesh Joshi
 

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

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top