Problems with C + + returning references to local variables

Source: Internet
Author: User

Conclusion:

(1) You cannot return a reference variable to a local variable.

(2) However, it is possible to return a reference to a related non-reference variable.

The reasons are as follows:

Question one:
Returns a reference to a local variable. Such as:
int& ABC ()
{
int n = 5;
return n;
}
void Main ()
{
int x;
X=ABC ();
cout<<x<<endl;
}
Why is it possible to receive a reference to return a local variable? is local variable n not destroyed?

Explain:

(1)

The so-called cannot return local variable, refers to the local variable allocates the memory in the stack space, the function returns the stack pointer to fall back, when the main function continues to call other tuned functions, the stack pointer moves up, the space allocated by the last function call will be overwritten by this call, and if you refer to the original local variable at this time, unpredictable results will
So a local variable is not destroyed when the function returns but is equivalent to being reused. so if the first question you call a function before cout<<x<<endl;, this function allocates a lot of local variables, the output value will be changed later!

(2)
If the returned reference is assigned to a variable, no matter how it is changed, because the variable is copied (not necessarily called the constructor) when it is returned, I have already said that as long as you call a function before cout<<x and the local variable is defined within the function:
int x= ABC ();
Test ();
cout<<x;

void Test ()
{
int a=8;
}
So the output of X will be 8, the next question is similar to no longer an example.

Another explanation:

When the function exits, the local variable is stacked, and the object is definitely destroyed. destruction means that the memory is marked as idle for use by other variables, and the value on that memory still exists . After the release of the memory is not immediately occupied by other places, according to the actual situation will be idle for a while, may be a moment. If your program is more complex, the memory may be used again soon, and if the program is simple, it may be idle for a long time. So after the function returns the value of the variable is immediately assigned to another variable, the local variable has been destroyed, the memory of the value has not been modified, so your external variable obtained a correct value.

So your results right doesn't mean the variables aren't being destroyed. Just like the first one, if you call a function before cout<<x<<endl;, this function allocates a large number of local variables, that is, the value of that piece of content will change, which has been verified by me.

  The difference between a reference and an object is that the object needs to be constructed, and the reference is just an alias, like a pointer (the reference is actually a pointer in the C + + layer), so it is not possible to assign the returned function to a reference because it points to memory that might already be occupied by other content. It is possible to assign the returned reference to a variable, because the variable is copied when it is returned.

See the following example:

#include <iostream>
using namespace Std;
int& Fun ()
{
int i = 100;
return i;
}
void Test ()
{
int i = 0;
}
int main ()
{
int& a = Fun ();// Returns a reference to the local variable I, which is the address, has a problem. equivalent to int &a = i;

int A=fun ();// Returns the value of the local variable i, no problem, equal to int a = i;
Test (); There is no other code here because of the fun, so the output value is not a problem, if the test function is opened, then the value of a will be problematic.
cout << a << Endl;
return 0;
}

Example 2:

#include <iostream>
using namespace Std;

Class Complex
{
Public
Complex (double r=0,double i=0)
{
Real=r;imag=i;
}
Complex (const complex& c);
complex& operator+ (Complex &);
void display ();
Private
Double real, imag;
};
Complex::complex (const complex& c)
{
Real=c.real;
Imag=c.imag;
}
complex& complex::operator+ (Complex &c2)
{
int a=1;
Complex C;
C.real=real+c2.real;
C.imag=imag+c2.imag;
return C;
}
void Complex::d isplay ()
{
int xxxx=1;
cout << "(" <<real << "+" <<imag << "i)" <<endl;
}

int main ()
{
Double x, y;
cin>>x>>y;
Complex comp1 (x, y), COMP2 (y,x), Comp3;
Comp1.display ();
Comp2.display ();

comp3=comp1.operator+ (COMP2); //The reference returned with the object receive function is correct
complex& p=comp1.operator+ (COMP2); The reference that is returned by the object's reference receive function is incorrect

P.display ();
Comp3.display ();
return 0;
}

Problems with C + + returning references to local variables

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.