Several situations where a function returns a local variable __c

Source: Internet
Author: User

Reprint http://blog.csdn.net/haiwil/article/details/6691854/

In this paper, we discuss several cases of returning local variables in detail, which is worthy of our attention.

To be precise, a function cannot return pointers to stack memory (note that this refers to the stack, and it is possible to return pointers to heap memory).

Here are some typical examples of a function that returns a pointer to a local variable:

1:

[CPP] view plain copy #include <stdio.h> char *returnstr () {char *p= "Hello world!";    return p;        int main () {char *str;        Str=returnstr ();        printf ("%s\n", str);    return 0; }

This has no problem, because "Hello world!" is a string constant, stored in a read-only segment, that assigns the first address of the read-only segment of the string constant to the pointer, so the RETURNSTR function exits with the memory of the string constant not being reclaimed, so it can be accessed smoothly through the pointer.

2:

[HTML] view plain copy #include <stdio.h> char *returnstr () {char p[]= "Hello world!";    return p;        int main () {char *str;        Str=returnstr ();        printf ("%s\n", str);    return 0; }

"Hello world!" Is that local variables are stored in the stack. When the RETURNSTR function exits, the stack to empty, the local variable memory is also emptied, so the function returned is a freed memory address, so it is possible to print out the garbled .

3:

[HTML] view plain copy int func () {int A;    ... return A;         Allow} int * Func () {int A;    ... return &a; Meaningless, should not do so}

Local variables also branch automatic variables and local static variables, because a returns a value, so it is possible to return a local variable, whether automatic or static,

Because the value of this local variable is returned at this time, it should not return a pointer to a local automatic variable, because the local automatic variable after the function call ends

Being discarded, this pointer points to an object that no longer exists and is meaningless. But you can return a pointer to a local static variable because the static variable survives

period from the definition to the end of the program.
4: If the return value of a function is not the address of a local variable, then the local variable must be declared as a static type. as follows: [HTML] view plain copy #include <stdio.h> char *returnstr () {static char p[]= "Hello W        Orld! ";    return p;         int main () {char *str;        Str=returnstr ();           printf ("%s\n", str);    return 0; }

5: An array cannot be the return value of a function because the compiler considers the array name to be the address of a local variable (array). Returning an array is generally replaced with a pointer to the array, and the pointer cannot point to an automatic array because the automatic array is discarded after the function is finished, but a pointer to a static local array can be returned, because the static storage period is defined from the object to the end of the program. As follows:

[HTML] view plain copy int* func (void) {static int a[10];

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.