Do I need to take care of memory allocation, scope and deletion of C++ strings allocated by a literal?
For example:
#include <string>
const char* func1() {
const char* s = "this is a literal string";
return s;
}
string func2() {
std::string s = "this is a literal string";
return s;
}
const char* func3() {
std::string s = "this is a literal string";
return s.c_str();
}
void func() {
const char* s1 = func1();
std::string s2 = func2();
const char* s3 = func3();
delete s1; //?
delete s3; //?
}
func2
: I don't need todelete s2
.func3
: Do I need todelete s3
?
Is func1
correct? Is character memory content still available after it leaving func1
's scope? If yes, should I delete it when I do not need it anymore?