Some insights on the return value of a function

Source: Internet
Author: User

We all know that the C + + function return type has three return values that return a pointer return reference.
Here's a brief talk about what happened when the function returned.

return value type:
Take a very simple example:
function definition:

int get ()
{
int n=3;
return n;
}

int main ()
{
int K=get ();
}

Looking at the Get function first, the local variable n is a value generated in the stack, so the space is freed at the end of the function, and the end of the function means
Before or after return.

The answer is not necessarily, it is generally not released immediately.
Return this action will copy the value of N to a temporary variable q, which acts as the return value of the function, until the assignment of the main function succeeds, the temporary variable is destroyed.
So actually in the main function The K=get () k is the value of the temporary variable that gets the get () function, and this value does exist, so the assignment succeeds.
After K successfully obtains the value of Q, the TEMP variable q and local variable n may be destroyed or not destroyed, which is indeterminate.



Returns the pointer type:

int *get ()
{
int *p = new int (3);
return p;
}

int main ()
{
int *k=get ();
}

Also see get function first, local variable p applied for a piece of dynamic memory, the value is 3. The actions that take place here in return P are:
The function generates a temporary variable, which is an int *, which we assume to be Q
Q = p; That is, Q points to the place where P is pointing, and here is the dynamic memory that p applies for.
Temp variable q acts as the return value of a function
So in the main function k = Get () is actually k = q when k points to the memory of the dynamic request.
Similarly the temporary variable Q completes the task and the address of the local variable p is invalid, but their value still exists.

So it is possible to return a local pointer to dynamic memory.


Error Usage: Returns a pointer to a local object.

int *get ()
{
int n = 3;
Return &n;
}

int main ()
{
int *p = NULL;
int *k = NULL;
p = k = Get ();
cout<<*p<<endl;
cout<< "Operation" <<endl;
cout<<*p<<endl;
}
Here the Get function returns the address of N.
The same copy occurs when the Get function returns, assuming that the TEMP variable is named temp int *temp = &n;
At this point, temp does point to the address of N and is returned as the return value of the function.
Look at the main function here I used a chain copy to better illustrate the problem.
First See k = Get (), that is, K =temp occurred; At this point, K also points to the value of function N.
Then P = K,p also points to the value of the function local variable n.
Cout<<*p magically found out 3 indeed. You may wonder if the value of the local variable n has not been released yet.
It is still not released, because n is a temporary variable, and the address of n is recycled by the system.
But its value has not been written to another content, so the value of 3 still exists.The reason for this is that a compiler is involved in the efficiency of using memory, that is, when the copy is written, and when this memory is needed, it is written without intermediate release.
But in the second execution cout<<*p<<endl; It is found that the output is no longer 3 but a large number of values.
This is the explanation3 Memory consumed by this value is rewritten。 So the explanation is: after the first Cout<<*p<<endl is executed, when the system is running for a period of time, it uses the reclaimed memory for other purposes in the process of calling << function, outputting results, etc., and the corresponding value of the address is changed to an invalid value.
So this is an indeterminate question, and you don't know when the compiler will release the value of this temporary variable n, so there's a security risk.

The same is true for returning local references.

Some insights on the return value of a function

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.