const ...">

String allocation in C ++

Do I need to take care of memory allocation, volume and deletion of a "++" C ++ object?

eg:

#include <string> const char* func1() { const char* s = "this is a literal string"; return s; } string func2() { std::string s = "this is a literal string"; return s; } const char* func3() { std::string s = "this is a literal string"; return s.c_str(); } void func() { const char* s1 = func1(); std::string s2 = func2(); const char* s3 = func3(); delete s1; //? delete s3; //? } 

func2: I don’t need to “delete” s2. func3: do i need to "remove s3"?

btw, is the func1 function correct? Is the contents of the character memory saved after it leaves the func1 area? If so, should I remove it when I no longer need it?

+7
c ++ string memory
source share
4 answers
  • func1() returns a pointer to a string literal. You must not delete string literals.
  • func2() (presumably you omitted the std:: prefix), returns a std::string . He takes care of himself.
  • func3() returns a pointer to a string that is controlled by the std::string object, which is destroyed when the function exits. You should not touch this pointer after the function returns.
  • You will need to take care of the memory returned by this function:

     const char* func4() { char* s = new char[100]; // fill char array with a string return s; } 

However, manual resource management is difficult. To begin with, if a function returns a bare pointer, you don’t know whether it points to a single object ( char ) or its array, and whether it needs to be deleted. You should avoid all this and just stick with std::string .

+16
source share

You have another problem with s3, namely that the func3 () function returns a pointer to an object that goes out of scope when the function returns. Do not do this.

To clarify: your local string object in func3 () will cease to exist when the function returns, so there is no need to delete it. However, you still have a pointer to your internal buffer, which you are returning. You cannot use this.

A very good and detailed past answer is here so that there is no more confusion: Is it more efficient to return a link to a constant

+3
source share

I discard the corresponding code for each function and process its return value and comment below:

 const char* func1() { const char* s = "this is a literal string"; return s; } const char* s1 = func1(); delete s1; //? 

You cannot delete s1 , because the line pointed to does not live on the heap.

 string func2() { string s = "this is a literal string"; return s; } string s2 = func2(); 

This is normal. func2 s goes beyond and clears. s2 will duplicate the line from s , and also clear it at the end of func .

 const char* func3() { string s = "this is a literal string"; return s.c_str(); } const char* s3 = func3(); delete s3; //? 

func3 returns a pointer to a string that has been freed. You will get a double free exception when doing delete s3 .

+1
source share

In func3, your local string is created by a compiler that calls the implicit constructor string(const char*) , initializing its internal buffer with a copy of the string literal. Then you return a pointer to the internal string buffer, which quickly goes out of scope and freed as soon as the function returns.

+1
source share

All Articles