简体   繁体   English

内存中分配的 std::string 在哪里?

[英]Where is a std::string allocated in memory?

Here is a function:这是一个函数:

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

When we declare the string inside the function, is it stored on the Stack or Heap?当我们在函数内部声明字符串时,它是存储在堆栈还是堆中? Why?为什么?

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

Can we return the string reference and continue using somewhere else in the program?我们可以返回字符串引用并继续在程序中的其他地方使用吗?

When we declare the String inside the function, is it stored on the Stack or Heap?当我们在函数内部声明 String 时,它是存储在 Stack 还是 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.字符串构造对象的实现使用堆上的内存。

The object str (it is the instance of the class std::string ) is allocated in the stack.对象str (它是类std::string的实例)在堆栈中分配。 However, the string data itself MAY BE allocated in the heap.但是,字符串数据本身可以分配在堆中。 It means the object has an internal pointer to a buffer that contains the actual string.这意味着该对象有一个指向包含实际字符串的缓冲区的内部指针。 However, again, if the string is small (like in this example) usually the string class will have what we call "small string optimization".然而,如果字符串很小(如本例中),通常字符串类将具有我们所说的“小字符串优化”。 It means that the size of the std::string object itself is enough to contain the data of the string if it's small enough (usually around 23 bytes + 1 byte for null-terminator)... then if the content is bigger than this, the string data will be allocated in the heap.这意味着std::string对象本身的大小足以包含字符串的数据,如果它足够小(通常大约 23 个字节 + 1 个字节用于空终止符)......然后如果内容大于这个,字符串数据将被分配在堆中。

Usually you can return your string normally.通常你可以正常返回你的字符串。 C++ can handle this for you. C++ 可以为您处理这个问题。 The move semantics can take care of whatever necessary here to return the string object pointing to the same string data of the original string, avoiding doing unecessary copies.移动语义在这里可以处理任何需要返回指向原始字符串相同字符串数据的字符串对象,避免进行不必要的复制。

"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. "StackOverflo" ,字符串文字,可能存储在二进制文件的只读数据空间中,并在程序启动时映射到内存中。 You can read more about this here .您可以在此处阅读更多相关信息。

str , the class instance`, is allocated on the stack. str ,类实例str ,在堆栈上分配。 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. foo函数返回str副本,因此您编码的内容没问题。

all local variables in C++ are stores in Stack. C++ 中的所有局部变量都存储在堆栈中。 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.因此,您的str存储在 Stack 中,而其值存储在 Heap 中。 because std::string uses char pointer to allocate your "stackoverflow"因为std::string使用char指针来分配您的"stackoverflow"

On Windows platform, in C++, with std::string, string size less than around 14 char or so (known as small string) is stored in stack with almost no overhead, whereas string size above is stored in heap with overhead.在 Windows 平台上,在 C++ 中,使用 std::string,小于 14 个字符左右的字符串大小(称为小字符串)几乎没有开销地存储在堆栈中,而上面的字符串大小存储在堆中时有开销。 Usually platform and compiler dependent which can be tweaked with optimization options.通常取决于平台和编译器,可以使用优化选项进行调整。

std::string myStr {"Hello World"}; std::string myStr {"Hello World"}; // stored in program stack std::string myStr {"StackOverflow saved life of a programmer" }; // 存储在程序栈中 std::string myStr {"StackOverflow 挽救了程序员的生命" }; //pointer is stored in program stack whereas memory for string and is allocated in heap //指针存储在程序栈中,而字符串存储在堆中

Below link has good explanation.下面的链接有很好的解释。 https://blogs.msmvps.com/gdicanio/2016/11/17/the-small-string-optimization/ https://blogs.msmvps.com/gdicanio/2016/11/17/the-small-string-optimization/

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() .在堆上分配的最大可能也是唯一的原因是允许字符串对象的大小增长,例如通过调用push_back()append() Objects stored on the stack cannot change its size - that's defined by C/C++ language.存储在堆栈上的对象不能改变它的大小——这是由C/C++语言定义的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM