Here is a function:
void foo() {
string str = \"StackOverflo\";
str.push_back(\'w\');
}
When we declare the string inside the function, i
"StackOverflo"
, the string literal, is likely stored in the read-only data space of the binary and is mapped into memory when the program starts. You can read more about this here.
str
, the class instance`, is allocated on the stack. But the memory its constructor allocates to make a copy of the string literal is allocated on the heap.
The foo
function returns a copy of str
, so what you have coded is ok.