devo occuparmi dell'allocazione della memoria, dell'ambito e dell'eliminazione dell'oggetto "stringa" C++?allocazione stringa C++
ad esempio:
#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: Non ho bisogno di 'eliminare' s2. func3: devo cancellare 's3'?
btw, is func1 è corretto? Il contenuto della memoria dei caratteri è ancora disponibile dopo aver abbandonato l'ambito func1? Se sì, dovrei eliminarlo quando non ne ho più bisogno?
http://stackoverflow.com/questions/2579874/lifetime-of-a-const-string-literal-returned-by-a-function – elgcom
http://stackoverflow.com/questions/267114/scope-of -string-letterals – elgcom