In the description of the string::clear function, it says:
clear: Erases the contents of the string, which becomes an empty string (with a length of 0 characters).
In the description of the list::clear function, it says:
clear: Removes all elements from the list container (which are destroyed), and leaving the container with a size of 0.
Does the clear overwrite the memory of the string and the list or just free them?
Problem Description. The String::clear() function does only set the length to 0, it does not free the memory.
No, memory are not freed. In C++11, you can use the shrink_to_fit method for force the vector to free memory.
std::vector does call the destructor of every element it contains when clear() is called. In your particular case, it destroys the pointer but the objects remain.
The std::string type is the main string datatype in standard C++ since 1998, but it was not always part of C++. From C, C++ inherited the convention of using null-terminated strings that are handled by a pointer to their first element, and a library of functions that manipulate such strings.
Neither function is required to overwrite the erased data.
The memory isn't overwritten. It is not even guaranteed to be freed.
For example, if you create a huge string and call clear
on it, only its size will be reduced, but the allocated memory may still be reserved. However, it will be freed if the string gets out of scope.
std::list
at least guarantees that the elements inside the list will be destructed if you clear the list.
So, if your memory contains sensitive data, you should manually overwrite them.
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