Where is a std::string allocated in memory?

后端 未结 6 1638
我在风中等你
我在风中等你 2021-02-01 15:02

Here is a function:

void foo() {
   string str = \"StackOverflo\";
   str.push_back(\'w\');
}

When we declare the string inside the function, i

6条回答
  •  旧时难觅i
    2021-02-01 15:39

    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.

提交回复
热议问题