C language, the function returns a pointer to non-stack memory pointer

Prerequisites: Classification memory

C / C ++ program memory occupied divided into two categories: 静态存储区with 动态存储区. Which mode is shown below:

The difference between the data stored in static memory and dynamic memory is: static storage area in 编译-链接phase have been identified, the program is running will not change, only when the program exits, the memory will be static storage area recovery system. Dynamic memory is allocated dynamically during program operation.

In other places we can see there are other memory allocation classification, classification of all those segments, such as word constant region, such as global data area, the property is a static memory that category.

On the classification of memory here just roughly say something, about memory contents can be viewed in more detail Past notes: [C language] memory summary notes

Examples: return pointer returns a pointer to the stack memory

Look at a return returns a memory pointer points to the stack of example:

#include <stdio.h>

char *GetStr(void)
{
    char p[] = "Hello"; /* 保存在栈中 */
    return p;
}

int main(void) 
{
    char *str = NULL;
    str = GetStr();
    printf("%s\n", str);
    return 0;
}

Program is compiled, the results of operation are as follows:

You can see, the compiler warning:

warning: function returns address of local variable

Our operating results are not expected output string Hello.

That is because GetStrthe function returns a pointer points to the stack memory, where the variable p is a local variable, and local variables are allocated on the stack. That is Hellostored on the stack memory, stack memory will be automatically destroyed at the end of the function call, so at this time the contents of p is unknown, so the result is no output.

Let the GetStrfunction amended as follows:

char *GetStr(void)
{
    char *p = "Hello";  /* p在栈上,Hello在静态区(常量区) */
    return p;
}

The results compiled to run at this time of what is it? The results are:

You can see the normal output. Why is it here and can output normal? P here because although allocated on the stack, but this time Hellois a string constant, which is stored in static memory. In calling GetStrthe end of the function it will not be destroyed.

There may be some people have doubts, also Hello, why in a stack, in a static area.

char *p = "Hello";

Here first define a pointer variable p, the compiler will open up space for the stack pointer variable. And at this time and no space to store Hello, it Hellocan only be stored in a static area.

char p[] = "Hello";

Here a first defined array of p, because the array size is not given, the array size is not determined at this time. And then Hellostored in the array, the compiler will be an array of p open and close enough stack space to store Hello.

Related notes: char * str to char str [] difference

Other alternatives

From the above example we know that if the function returns a pointer to a pointer to the stack memory, the results obtained are not what we want. In addition to the above method, where there are several solutions:

1, the p is defined as a global variable, because global variables are stored in static memory area, the program will end release. However, this causes the function is not reentrant. About reentrant and non-reentrant functions may view the Past notes: What is reentrant function?

2, GetStruse the function mallocto apply dynamic memory, but we must remember to use the used freeto be released, otherwise it will lead to memory leaks. Sample code is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *GetStr(void)
{
    char *p = (char*)malloc(64*sizeof(char));
    strcpy(p, "Hello");
    return p;
}

int main(void) 
{
    char *str = NULL;
    str = GetStr();
    printf("%s\n", str);
    free(str);  /* 释放str指向的堆内存 */
    return 0;
}

3, p variables can be declared as static static variables. But it also causes the function is not reentrant. Sample code is as follows:

char *GetStr(void)
{
    static char p[] = "Hello";
    return p;
}

End: Those are the times to share notes, if wrong, welcome place! If this note to help you, welcome to the collection, forwarding, watching ~


Scan code or press attention

Reply to "  basketball belly " into the technical group chat

发布了395 篇原创文章 · 获赞 788 · 访问量 72万+

Guess you like

Origin blog.csdn.net/weiqifa0/article/details/103724732