C++ management of strings allocated by a literal

后端 未结 4 1518
终归单人心
终归单人心 2020-12-10 15:44

Do I need to take care of memory allocation, scope and deletion of C++ strings allocated by a literal?

For example:

<         


        
4条回答
  •  情书的邮戳
    2020-12-10 16:27

    You have a different problem with s3, namely that the function func3() returns a pointer into an object that goes out of scope when the function returns. Don't.

    To clarify: Your local string object within func3() will cease to exist on return of the function, so no need to delete. However, you still have a pointer to its internal buffer, which you return. You can't use that.

    Very good and detailed past answer here, lest more confusion ensues: Is it more efficient to return a const reference

提交回复
热议问题