return address of temporary variable?

T

thomas

-----code----
vector<int> &calc(){
vector<int> temp;
temp.push_back(3);

return temp;
}
----code----

"temp" is a temporary variable, when returned, it's like return by
alias.
So is the code above legal?
 
A

Alf P. Steinbach

* thomas:
-----code----
vector<int> &calc(){
vector<int> temp;
temp.push_back(3);

return temp;
}
----code----

"temp" is a temporary variable, when returned, it's like return by
alias.
So is the code above legal?

If you mean valid according to the C++ standard, yes, but a good compiler will
produce a warning.

It's not a good idea to use the result.


Cheers, & hth.,

- Alf
 
A

Abdullah

-----code----
vector<int> &calc(){
vector<int> temp;
temp.push_back(3);

return temp;}

----code----

"temp" is a temporary variable, when returned, it's like return by
alias.
So is the code above legal?

No, since temp will be destroyed when the function returns. The
returned reference will be a dangling one.
 
T

thomas

well, thanks.
but if I put the "temp" vector out of the function, it seems a little
ugly.
so I want it to be clean, and i want to "new" a vector in the function
scope, and return the "*temp".
but it seems that i will have to delete it somewhere.

anyway, it seems not very neat. So is my feeling just wrong? or any
other way to write it to make me feel better?
 
J

Juha Nieminen

thomas said:
-----code----
vector<int> &calc(){
vector<int> temp;
temp.push_back(3);

return temp;
}
----code----

"temp" is a temporary variable, when returned, it's like return by
alias.
So is the code above legal?

If you mean "legal" as in "compiles", then the answer is yes. If you
mean "legal" as in "works", the answer is no.

Returning a reference to a temporary is basically no different than
returning a pointer to a temporary. In either case if you try to access
the pointed object, bad things will happen.

To do what you want you have several options. To do *exactly* what you
are wanting to do above, either return by value (instead of by
reference), or return a dynamically allocated instance (preferably using
a smart pointer or std::auto_ptr).
In the larger context other solutions may be better.
 
E

Erik Wikström

well, thanks.
but if I put the "temp" vector out of the function, it seems a little
ugly.
so I want it to be clean, and i want to "new" a vector in the function
scope, and return the "*temp".
but it seems that i will have to delete it somewhere.

anyway, it seems not very neat. So is my feeling just wrong? or any
other way to write it to make me feel better?

As Juhu Nieminen pointed out, returning a copy might be a good idea:

vector<int> calc(){
// ^^^ Notice the lack of &
vector<int> temp;
temp.push_back(3);

return temp;
}
 
J

John Dibling

Instead of returning a reference to a temporary, and instead of
returniing a copy of the vector, try instead passing in to your
function an iterator you can use to insert values in to the vecor.
For example:

#include <cstdlib>
#include <vector>

template<class InsertIterator> void calc(InsertIterator & ii)
{
*ii = 3;
}


int main()
{
typedef std::vector<int> ints;
ints myInts;

calc(std::back_insert_iterator<ints>(myInts));

return 0;
}
 

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,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top