473,466 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

return address of temporary variable?

-----code----
vector<int&calc(){
vector<inttemp;
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?
Mar 9 '08 #1
4 1822
On Mar 9, 7:14 pm, thomas <FreshTho...@gmail.comwrote:
-----code----
vector<int&calc(){
vector<inttemp;
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.
Mar 9 '08 #2
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?
Mar 9 '08 #3
thomas wrote:
-----code----
vector<int&calc(){
vector<inttemp;
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.
Mar 9 '08 #4
On 2008-03-09 15:36, thomas wrote:
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<intcalc(){
// ^^^ Notice the lack of &
vector<inttemp;
temp.push_back(3);

return temp;
}

--
Erik Wikström
Mar 9 '08 #5

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

Similar topics

8
by: DaKoadMunky | last post by:
Please consider the following... <CODE> #include <string> using namespace std; typedef int PrimitiveType; typedef string ClassType;
25
by: cppaddict | last post by:
I'd like to know what goes on under the hood when methods return objects. Eg, I have a simple Point class with two members _x and _y. It's constructor, copy constructor, assignment operator and...
2
by: erictham | last post by:
#include <iostream> #include <cmath> using namespace std; // implements a pseudo RNG generator by the linear congruential generator const int n1=100; double *LRNG(int, int, int); int main()...
33
by: baumann.Pan | last post by:
hi all, i want to get the address of buf, which defined as char buf = "abcde"; so can call strsep(address of buf, pointer to token);
11
by: igor.lautar | last post by:
Hi, How can I get the address of return value in function? ex. int function(int argument) { int *arg_address; int *rtn_address;
4
by: Tom | last post by:
I come from C but don't really understand why in C++ you can return a temporary object from a function. For example: class Test { public: Test() { cout << "Address of object: " <<this...
10
by: flopbucket | last post by:
Hi, Is this legal? std::string foo() { std::string xyz = "FOO"; return xyz; }
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
4
by: James Kanze | last post by:
On Nov 18, 5:50 pm, Pete Becker <p...@versatilecoding.comwrote: Has this changed in the latest draft. According to my copy of the standard (version 1998---out of date, I know), "The...
0
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
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,...
0
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
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
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
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,...
0
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
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...
0
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...

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.