Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly free a std::string from memory

Tags:

c++

string

What's the best way to delete an std::string from memory allocated on the heap when I'm done using it? Thanks!

like image 585
bbosak Avatar asked Feb 12 '11 18:02

bbosak


People also ask

How do I free std::string?

std::string is just a normal class1, so the usual rules apply. If you allocate std::string objects on the stack, as globals, as class members, ... you don't need to do anything special, when they go out of scope their destructor is called, and it takes care of freeing the memory used for the string automatically.

How is std::string stored in memory?

unless you actually mean that std::string is a type (instantiation of a template) from which objects are instantiated... @user1145902: They are stored in memory like in an array, but that memory is not allocated in the stack (or wherever the string object is), but rather in a dynamically allocated buffer.

Do I need to free C++ string?

The char* string exists on its own and is unaffected and indeed has its own memory, not shared with the newly created std::string class. You will still need to delete the C-string if it is using malloced memory. Use free for such strings if they are C-style strings created with malloc .

How do I free all memory in CPP?

C++ free() The free() function in C++ deallocates a block of memory previously allocated using calloc, malloc or realloc functions, making it available for further allocations. The free() function does not change the value of the pointer, that is it still points to the same memory location.


Video Answer


2 Answers

std::string is just a normal class1, so the usual rules apply.

If you allocate std::string objects on the stack, as globals, as class members, ... you don't need to do anything special, when they go out of scope their destructor is called, and it takes care of freeing the memory used for the string automatically.

int MyUselessFunction() {     std::string mystring="Just a string.";     // ...     return 42;     // no need to do anything, mystring goes out of scope and everything is cleaned up automatically } 

The only case where you have to do something is when you allocate an std::string on the heap using the new operator; in that case, as with any object allocated with new, you have to call delete to free it.

int MyUselessFunction() {     // for some reason you feel the need to allocate that string on the heap     std::string * mystring= new std::string("Just a string.");     // ...     // deallocate it - notice that in the real world you'd use a smart pointer     delete mystring;     return 42; } 

As implied in the example, in general it's pointless to allocate a std::string on the heap, and, when you need that, still you should encapsulate such pointer in a smart pointer to avoid even risking memory leaks (in case of exceptions, multiple return paths, ...).


  1. Actually std::string is defined as

    namespace std {     typedef std::basic_string<char> string; }; 

    so it's a synonym for the instantiation of the basic_string template class for characters of type char (this doesn't change anything in the answer, but on SO you must be pedantic even on newbie questions).

like image 176
Matteo Italia Avatar answered Sep 28 '22 18:09

Matteo Italia


std::string foo("since it's on the stack, it will auto delete out of scope"); 

or:

std::string* foo = new std::string("allocated on the heap needs explicit destruction") delete foo; 
like image 22
The Communist Duck Avatar answered Sep 28 '22 18:09

The Communist Duck