473,480 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

assigning reference to a returned value from function.

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<intretIntVector()
{
vector<intvecInt;
/*
//Populate the vector with int values.
*/
...
....
return vecInt;

}
//end function Definition
int main()
{
vector<intvect_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

Aug 8 '06 #1
12 1862
yp*********@indiatimes.com wrote:
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<intretIntVector()
{
vector<intvecInt;
/*
//Populate the vector with int values.
*/
..
...
return vecInt;

}
//end function Definition
int main()
{
vector<intvect_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>& );

--
Ian Collins.
Aug 8 '06 #2
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

Aug 8 '06 #3
yp*********@indiatimes.com wrote:
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<intretIntVector()
{
vector<intvecInt;
/*
//Populate the vector with int values.
*/
..
...
return vecInt;

}
//end function Definition
int main()
{
vector<intvect_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;
}
Aug 8 '06 #4
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<intv = retIntVector();

or:

vector<intconst v = retIntVector();
The solution I'd got for is the following. I'd change the function into a
constructor:

struct VecRetriever {

vector<intvec;

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

int main()
{
VecRetriever vecr;

vecr.vec[0] = ...
}

--

Frederick Gotham
Aug 8 '06 #5
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<intvec;

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::valuemem;

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

/* Do some stuff */

vec.~vector();
}

--

Frederick Gotham
Aug 8 '06 #6
yp*********@indiatimes.com wrote:
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<intretIntVector()
{
vector<intvecInt;
/*
//Populate the vector with int values.
*/
..
...
return vecInt;

}
//end function Definition
int main()
{
vector<intvect_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/articl...p?p=25033&rl=1

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

Cheers! --M

Aug 8 '06 #7

yp*********@indiatimes.com wrote:
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<intv(1000, 5) ;
return v ;
}

int main()
{
std::vector<intv ;
f().move_to(v) ;
}
Other than syntax, the obvious problem is that functions can only
return named objects.

--
Alan Johnson

Aug 8 '06 #8
Alan Johnson wrote:
yp*********@indiatimes.com wrote:
>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<intv(1000, 5) ;
return v ;
}

int main()
{
std::vector<intv ;
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<intf(); //

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

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 8 '06 #9

Victor Bazarov wrote:
Alan Johnson wrote:
yp*********@indiatimes.com wrote:
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<intv(1000, 5) ;
return v ;
}

int main()
{
std::vector<intv ;
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<intf(); //

int main() {
std::vector<intv;
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.

--
Alan Johnson

Aug 8 '06 #10

<yp*********@indiatimes.comskrev i meddelandet
news:11*********************@n13g2000cwa.googlegro ups.com...
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<intretIntVector()
{
vector<intvecInt;
/*
//Populate the vector with int values.
*/
..
...
return vecInt;

}
//end function Definition
int main()
{
vector<intvect_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
Aug 8 '06 #11
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

Aug 9 '06 #12

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<intfun();
Now the user can collect it in vector<intor const vector<int&.

Regards,
Yogesh Joshi

Aug 10 '06 #13

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

Similar topics

9
11889
by: mjm | last post by:
Folks, Stroustrup indicates that returning by value can be faster than returning by reference but gives no details as to the size of the returned object up to which this holds. My question is...
37
3919
by: Dave | last post by:
Hello all, Please consider the code below. It is representative of a problem I am having. foo_t needs to contain a bar_t which is a class without a copy constructor or operator=. It is not...
19
1938
by: JKop | last post by:
When I compile and run the following on my system: #include <iostream> static int hello = 78; int ReturnValue(void) {
25
9490
by: Sourav | last post by:
Suppose I have a code like this, #include <stdio.h> int *p; void foo(int); int main(void){ foo(3); printf("%p %d\n",p,*p);
5
2508
by: Mike Cain | last post by:
Hi - I am looking for the most efficient way to pass a STL string from one function to another (using MS VS 7.0 ATL if that matters) and have a few questions abuot the principles at work here. ...
7
2502
by: Ron Goral | last post by:
Hello I am new to creating objects in javascript, so please no flames about my coding style. =) I am trying to create an object that will represent a "div" element as a menu. I have written...
29
3617
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
8
4529
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I have a large class with a lot of member variables. I also have a function in the class that I would like to change ALL Of the member variables. I am trying to assign...
10
3940
by: flopbucket | last post by:
Hi, Is this legal? std::string foo() { std::string xyz = "FOO"; return xyz; }
0
7039
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,...
0
6904
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...
0
7080
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...
1
6735
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6895
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5326
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,...
1
4770
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
2992
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.