In C ++, does a return string created from a local char array cause a memory leak or undefined behavior?

I am wondering if this could cause a memory leak or an undefined result in C ++?

string foo() { char tempArray[30]; strcpy(tempArray, "This is a test"); return string(tempArray); } 

I know this is bad in C, but I have not found a specific answer for C ++.

So everyone says no, but am I still confused when my memory is freed?

Lets say that I have this method that calls the above method

 void bar() { string testString = foo(); } 

At what point in the above code does the string object returned from foo () call its destructor? Is it right after copying to the testString object?

+4
source share
6 answers

What happens in your example is that the constructor with the signature

string ( const char * s );

. The constructor allocates new memory for a copy of the string and copies it to this new memory. The string object is then responsible for freeing its own memory when its destructor is called.

When you create a copy of a string, the copy constructor also allocates memory and makes a copy.

You should also take a look at the RAII pattern. This is how string memory management works.

+1
source

No, there is no memory leak, and you do not need to do all this, this is equivalent to your code

 string foo() { return "This is a test"; } 
+2
source

In this code:

 char tempArray[30]; 

tempArray is a variable with automatic storage time. When tempArray "falls out of scope", it is automatically destroyed. You copy the contents of this array (somewhat awkwardly) to std::string , and then return the string value by value. Then tempArray destroyed. It is important to note that tempArray is an array . This is not a pointer to the first element of the array (as a rule, it is perceived incorrectly), but the array itself. Since tempArray destroyed, the array is destroyed.

A leak would occur if you used a variable with dynamic storage duration, for example:

  char* tempArray = new char[30]; strcpy(tempArray, "This is a test"); return string(tempArray); 

Note new[] without matching delete[] . Here tempArray is still a variable with automatic storage time, but this time it is a pointer, and what it indicates has a dynamic storage duration. In other words, tempArray destroyed when it goes out of scope, but it's just a pointer. What it points to is an array of char is not destroyed because you do not delete[] it.

+2
source

Not particularly effective, but no, no leaks.

+1
source

No, this will not cause any leaks, since you never allocate memory on the heap. If you used malloc , calloc or new .. and you are never free / delete . Then yes, a memory leak!

The array is statically allocated, so it is created on the stack. strcpy does not return a dynamically allocated object, it fills an existing array, it knows how to do this because of the pointer you passed in - again, not allocated on the heap. A copy of the string object is executed when you return the string.

+1
source

You can always return a local automatic variable from a function by value. For any type, this should be safe (if the copy constructor is not safe):

 T foo() { return T(...); } 

or

 T foo() { T t return t; } 

Your example matches my first case.

What is unsafe returns a pointer / link to a local automatic variable:

 T& foo() { T t return t; } T* foo() { T t return &t; } 
0
source

Source: https://habr.com/ru/post/1446564/


All Articles