c++ function returns string pointer

wrong solution

char* testfunc1(){
    char chp[10] = "cpp";
    return chp;
}

int main(int argc, const char * argv[]) {
    // insert code here...
    char *p = testfunc1();
    printf("%s\n",p);
    p = nullptr;
    return 0;
}

In the above code, a character array is created in the function, and the name of the character array is directly passed as a pointer of char type as the return value.

This approach is wrong, and the compiler will pop up a warning: Address of stack memory associated with local variable 'chp' returned. It means that the local variables on the stack are returned, which is not recommended in c++.

The reason for the problem is that the character array chp in testfunc1 is a local variable and is located in the stack area of ​​the memory, so the pointer *p points to the local variable in the stack. When the function finishes running, the block of memory is likely to be used by other codes. At that time, what is stored in the memory is not what we need.

The final result is that *p initially points to the memory where the string array chp is located. When the function is executed, *p still points to that memory, but this memory is still not necessarily the character array chp.

Conclusion: C++ functions should not return a pointer to a variable on the stack .

The solution is also very simple. The function returns a pointer to a variable that is not in the stack. In addition to the stack, we can actually place variables in other places artificially.

Solution 1: Return a pointer to a variable in the heap

//将字符串放在堆里
char* testfunc2(){
    char *p = new char(10*sizeof(char));
    strcpy(p, "hello!");
    return p;
}

int main(int argc, const char * argv[]) {
    // insert code here...
    char *p = testfunc2();
    printf("%s\n",p);
    delete p;
    p = nullptr;
    return 0;
}

In the function testfunc2, a block of memory is opened in the heap area, and strings are stored in it. In this way, the life cycle of the string has nothing to do with the function, even if the function is executed, *p still points to the string.

It should be noted that the data in the heap needs to be recycled manually, otherwise it will cause memory leaks.

Solution 2: Return a pointer to a static variable

//将字符串放在静态变量区
char* testfunc3(){
    static char p[100];
    strcpy(p, "hello!");
    return p;
}

int main(int argc, const char * argv[]) {
    // insert code here...
    char *p = testfunc2();
    printf("%s\n",p);
    p = nullptr;
    return 0;
}

The function testfunc3 creates a static string array, which is stored in the static variable area. The life cycle has nothing to do with the function, and it can also solve the problem.

It should be noted that the variables in the static variable area are unique. When the function testfunc3 is called from multiple places, the same variable is used. It is better to make this variable a constant.

Solution 3: Return a pointer to a global variable

//将字符串放在全局变量区
char pglobal[100];
char *testfunc4(char* s){
    strcpy(s, "hello!");
    return s;
}

int main(int argc, const char * argv[]) {
    // insert code here...
    char* p = testfunc4(pglobal);
    printf("%s\n",p);
    p = nullptr;
    return 0;
}

Using global variables is even more violent!

Guess you like

Origin blog.csdn.net/m0_37872216/article/details/126994791