Reputation: 1331
Can I free the memory of the char* pointed string after I have convert it to a std::string? For example:
char* c_string;
c_string = strdup("This is a test");
std::string cpp_string;
cpp_string(c_string);
free(c_string); /* can I call free here? */
Upvotes: 9
Views: 2102
Reputation: 137398
Yes. The std::string
constructor makes a copy of the string passed to it.
See constructor #4 on this page.
string (const char* s); // from c-string
from c-string
Copies the null-terminated character sequence (C-string) pointed by s.
Upvotes: 5
Reputation: 129001
Yes. std::string
copies the underlying C string.
Source: Table 67 of §21.4.2 of C++11 draft N3376.
Upvotes: 13