Where is a std::string allocated in memory?

后端 未结 6 1634
我在风中等你
我在风中等你 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:51

    all local variables in C++ are stores in Stack. even local pointers are stored in Stack. but the memory allocated in pointer are stored on Heap.

    So, your str is stored in Stack while its value is stored in Heap. because std::string uses char pointer to allocate your "stackoverflow"

提交回复
热议问题