Copy a string: Difference between revisions

Content added Content deleted
Line 155: Line 155:
std::string s = "Hello";
std::string s = "Hello";
// allocate memory for C string
// allocate memory for C string
char* cs = (char*)malloc(s.length()+1);
char* cs = (char*)std::malloc(s.length()+1);
// copy the string
// copy the string
std::strcpy(cs, s.c_str());
std::strcpy(cs, s.c_str());
Line 163: Line 163:
s = cs;
s = cs;
// get rid of temporary buffer
// get rid of temporary buffer
free(cs); // delete or delete[] may not be used here</lang>
std::free(cs); // delete or delete[] may not be used here</lang>


=== Using string types supplied by specific compiler-provided or external libraries ===
=== Using string types supplied by specific compiler-provided or external libraries ===