What's the best way to delete an std::string from memory allocated on the heap when I'm done using it? Thanks!
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.
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.
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 .
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.
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, ...).
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).
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With