Here is a function:
void foo() {
string str = \"StackOverflo\";
str.push_back(\'w\');
}
When we declare the string inside the function, i
When we declare the string inside the function is it stored on the Stack or Heap? Why?
The biggest and maybe the only reason to allocate on the heap is to allow string objects to grow in size, for example by calling push_back()
or append()
. Objects stored on the stack cannot change its size - that's defined by C/C++
language.