I want to know if this causes a memory leak:
std::string test() {
return *(new std::string(""));
}
I want to know if this causes a memory leak:
std::string test() {
return *(new std::string(""));
}
Yes, it's a memory leak. When the function returns, a copy is made of the original string object.
Then the original new'ed pointer falls out of scope and is lost - a leak.
To make it less leaking make it return a reference:
std::string& test() {
return *(new std::string(""));
}