Where is a std::string allocated in memory?

后端 未结 6 1688
我在风中等你
我在风中等你 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条回答
  •  误落风尘
    2021-02-01 15:34

    When we declare the String inside the function, is it stored on the Stack or Heap?

    The string object itself is stored on the stack but it points to memory that is on the heap.

    Why?

    The language is defined such that the string object is stored on the stack. string's implementation to construct an object uses memory on the heap.

提交回复
热议问题