Return of local variables in the function

Source: Internet
Author: User

In general, a function can return local variables. Otherwise, it is not necessary to use global variables. If global variables are used, is it necessary to return them? Then the function has no meaning! However, it should be noted that the return of the so-called local variable has a great connotation. What value can be returned without errors? In fact, you only need to observe one sentence: The function cannot return a pointer to the stack memory! Why? Because all returned values are copied! We know that the scope of local variables is inside the function. Once the function is executed, the local variables on the stack will be destroyed and the memory will be released. Therefore, the function returns a copy of the value of the local variable, which is correct. However, if the address of the local variable is returned, only the copy of the local variable pointer is returned. As the function stops running, the stack memory pointed to by the copy pointer has been released, pointing to an unknown region will cause a call error. What if the returned Pointer Points to the heap memory? There is no problem with such use. In the new space in the function, the delete space outside the function. However, this is not a good programming style. Try to perform the new and delete operations in the same scope. Otherwise, the caller must manually release the memory to test whether such an interface is very bad. If you do need to do this, pass the pointer in! Well, let's take a look at some typical examples to find out what to pay attention to when returning local variables. 1. Correct. The most normal condition. [Cpp] int returnValue (); int _ tmain (int argc, _ TCHAR * argv []) {std: cout <returnValue (); return 0 ;} char returnValue () {int value = 3; return value;} 2. error. The most normal error. Although the value is released, its value may not be cleared, so sometimes it seems that the result is correct, but the hidden danger is infinite. [Cpp] int * returnValue (); int _ tmain (int argc, _ TCHAR * argv []) {std: cout <* (returnValue ()); return 0;} int * returnValue () {int value = 3; return & value;} 3. correct. Don't be surprised. "HelloJacky" is a String constant stored in the read-only data segment. return str only returns the first address of the string in the read-only data segment. When the function exits, the memory of the string is not recycled, so it is normal. [Cpp] char * returnValue (); int _ tmain (int argc, _ TCHAR * argv []) {std: cout <returnValue (); return 0 ;} char * returnValue () {char * str = "HelloJacky"; return str;} 4. error. This "HelloJacky" is a local variable in the stack. When the function exits, the memory is released. Therefore, it is wrong to return the address of the local variable in the stack. [Cpp] char * returnValue (); int _ tmain (int argc, _ TCHAR * argv []) {std: cout <returnValue (); return 0 ;} char * returnValue () {char str [] = "HelloJacky"; return str;} 5. correct. If you have to return the address of a local variable, add static. [Cpp] char * returnValue (); int _ tmain (int argc, _ TCHAR * argv []) {std: cout <returnValue (); return 0 ;} char * returnValue () {static char str [] = "HelloJacky"; return str;} 6. the array name is the first address of a local variable. [Cpp] int * returnValue (); int _ tmain (int argc, _ TCHAR * argv []) {std: cout <* (returnValue ()); return 0;} int * returnValue () {int value [3] = {1, 2, 3}; return value;} 7. correct. Add the static modifier, And the array can also be returned. [Cpp] int * returnValue (); int _ tmain (int argc, _ TCHAR * argv []) {std: cout <* (returnValue ()); return 0;} int * returnValue () {static int value [3] = {1, 2, 3}; return value;} 8. correct. Apply for a space in the function and release the space after calling the function. The disadvantage is that the interface is not flexible. [Cpp] char * newMemory (int size); int _ tmain (int argc, _ TCHAR * argv []) {char * p = newMemory (2); if (p! = NULL) {* p = 'a';} std: cout <* p; delete [] p; return 0;} char * newMemory (int size) {char * p = NULL; p = new char [size]; return p ;}

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.