Kam
Kam

Reputation: 6008

Does std::string::clear and std::list::clear erases data from memory

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?

Upvotes: 9

Views: 3143

Answers (2)

Philipp Claßen
Philipp Claßen

Reputation: 43979

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.

Upvotes: 10

Pete Becker
Pete Becker

Reputation: 76315

Neither function is required to overwrite the erased data.

Upvotes: 11

Related Questions