CPP note_1, cpp_1

Source: Internet
Author: User

CPP note_1, cpp_1

This note records the local values in the returned function.

We know that the local variables created in the function end with the completion of the function call process, that is, the removal of the corresponding function stack frame, and its lifecycle. If we return this local variable, it is possible that the data corresponding to this variable has been cleared and an error has occurred. Therefore, pay attention to the returned values. What is the detailed situation? Conclusion: If a local variable defined in a function is returned in the form of a pointer or reference, an error is returned. If it is returned by passing the value, it will be okay, because it returns a copy of the object, which still exists after the function ends. With regard to the issue of creating replicas, the compiler now has an optimization operation called NRV to avoid creating replicas to save costs, please refer to the http://www.cnblogs.com/xkfz007/articles/2506022.html

  Returned by passing values

1 // Function Definition: returns the local variable 2 vector <int> getMyVector () 3 {4 vector <int> vec; 5 for (int I = 0; I <5; ++ I) 6 {7 vec. push_back (I); 8} 9 return vec; 10} 11 12 // external call 13 vector <int> vec = getMyVector (); 14 for (int I = 0; I <5; ++ I) 15 {16 cout <vec [I] <endl; 17 18}

01234 is correctly printed in the external call.

  Returned by reference

1 vector<int> & getMyVector()2 {3     vector<int> vec;4     for (int i = 0; i < 5; ++i)5     {6         vec.push_back(i);7     }8     return vec;9 }

During compilation, you will see a warning: warning c00002: returns the address of the local variable or temporary variable, and an error occurs when the function is executed.

  Returns in Pointer Mode

1 vector<int> * getMyVector()2 {3     vector<int> vec;4     for (int i = 0; i < 5; ++i)5     {6         vec.push_back(i);7     }8     return &vec;9 }

A warning will be generated and a memory access error will occur, as is returned in reference mode. However, if we change the above function definition:

1 vector<int> * getMyVector()2 {3     vector<int> *vec = new vector<int>();4     for (int i = 0; i < 5; ++i)5     {6         vec->push_back(i);7     }8     return vec;9 }

Then, the compiler does not bring up a warning and runs properly. Because all objects created using new are allocated to the stack and will not be automatically released with the introduction of function calls, the programmer needs to manually release the corresponding memory space.

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.