472,967 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,967 software developers and data experts.

return type of a function that returns a local variable

Hello,

I understand that if a function "f" has a local variable "a", and
after it returns, "a" vanishes. If "f" returns "a" as a result, then I
noticed the following:

1. if the return type is "a&", then compiler complains reference to
the local variable "a".
2. if the return type is "a", then everything works fine.

I think this is because in the first case, the return is copy-by-
reference, and we can't reference a local var. In the second case, it
is a copy-by-value, and it's correct because we can copy the value
from a local var, is this right?

Thanks!

Mar 22 '07 #1
2 2046
On 22 Mar, 12:11, "Jess" <w...@hotmail.comwrote:
Hello,

I understand that if a function "f" has a local variable "a", and
after it returns, "a" vanishes. If "f" returns "a" as a result, then I
noticed the following:

1. if the return type is "a&", then compiler complains reference to
the local variable "a".
2. if the return type is "a", then everything works fine.
Your description is a bit confused. In your first paragraph a is an
object, in your second a is a type. But I think I understand what you
mean.
I think this is because in the first case, the return is copy-by-
reference, and we can't reference a local var. In the second case, it
is a copy-by-value, and it's correct because we can copy the value
from a local var, is this right?
Yes.

// foo1 is bad. Don't do this.
int& foo1()
{
int i = 42;
return i;
}

// foo2 is OK.
int foo2()
{
int j = 0;
return j;
}

int main()
{
int& int_reference = foo1();
int int_value = foo2();
}

In the code above, in the function main, int_reference is initialised
as a *reference to* the int i inside function foo1. As you correctly
suspect, after foo1 returns i no longer exists and so int_reference
refers to nothing. It is a dangling reference. Any use of
int_reference has undefined behaviour.

int_value on the other hand is initialised with *a copy* of the int j
from inside function foo2. It doesn't matter that j no longer exists
after foo2 returns because the copy of j was taken while j did still
exist.

Gavin Deane

Mar 22 '07 #2
Thanks, that solves my problem! :)

Mar 22 '07 #3

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

Similar topics

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...
8
by: Ravindranath Gummadidala | last post by:
Hi All: I am trying to understand the C function call mechanism. Please bear with me as I state what I know: "every invocation of a function causes a frame for that function to be pushed on...
23
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
11
by: randomtalk | last post by:
hi, i have the following recursive function (simplified to demonstrate the problem): >>> def reTest(bool): .... result = .... if not bool: .... reTest(True) .... else: .... print...
7
by: ashu | last post by:
look at code #include<stdio.h> int *mult(void); int main(void) { int *ptr,i; ptr=mult; for(i=0;i<6;i++) { printf("%d",*(ptr++));
14
by: Alexander Stoyakin | last post by:
Hello! Please advise on the following issue. I have a function which returns 1 or 0 in multiple branches, and it must perform the same code before each return, like this: int fun() { if (some...
18
by: terminator(jam) | last post by:
consider: struct memory_pig{//a really large type: memory_pig(){ std::cout<<"mem pig default\n"; //etc... }; memory_pig(memory_pig const&){
13
by: hari | last post by:
Hi all, Is it legal to return a local variable from function. Regards Hari
7
by: asit | last post by:
#include <stdio.h> //#include <stdlib.h> int main() { int c; printf("c before call=%d\n",c); c=message(); printf("c after call=%d\n",c); return 0;
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.