C++ 字符串是如何存储的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9132502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:21:09  来源:igfitidea点击:

How are C++ strings stored?

c++stringmemory

提问by user1145902

Possible Duplicate:
std::string and its automatic memory resizing

可能的重复:
std::string 及其自动内存大小调整

I am just curious, how are strings stored in memory? for example, when I do this:

我只是好奇,字符串如何存储在内存中?例如,当我这样做时:

string testString = "asd";

it allocates 4 bytes, right? a + s + d + \0.

它分配了 4 个字节,对吗?a + s + d + \0.

But later, when I want to assign some new text to this string, it works, but I don't understand how. For example I do this:

但是后来,当我想为这个字符串分配一些新文本时,它起作用了,但我不明白如何。例如我这样做:

testString = "123456789"

Now it should be 10 bytes long. But what if there wasn't space for such string? let's say that fifth+sixth bytes from the beginning of string are taken by some other 2 chars. How does the CPU handles it? It finds completely new position in memory where that string fits?

现在它应该是 10 个字节长。但是如果这样的字符串没有空间怎么办?假设从字符串开头开始的第 5+6 个字节被其他 2 个字符占用。CPU是如何处理的?它在内存中找到了适合该字符串的全新位置?

回答by David Rodríguez - dribeas

This is implementation dependent, but the general idea is that the string class will contain a pointer to a region of memory where the actual contents of the string are stored. Two common implementations are storing 3 pointers (begin of the allocated region and data, end of data, end of allocated region) or a pointer (begin of allocated region and data) and two integers (number of characters in the string and number of allocated bytes).

这是依赖于实现的,但一般的想法是字符串类将包含一个指向存储字符串实际内容的内存区域的指针。两种常见的实现是存储 3 个指针(分配区域的开始和数据、数据结束、分配区域的结束)或一个指针(分配区域和数据的开始)和两个整数(字符串中的字符数和分配的数量)字节)。

When new data is appended to the string, if it fits the allocated region it will just be written and the size/end of data pointer will be updated accordingly. If the data does not fit in the region a new buffer will be created and the data copied.

当新数据附加到字符串时,如果它适合分配的区域,它将被写入并且数据指针的大小/结尾将相应地更新。如果数据不适合该区域,则将创建新缓冲区并复制数据。

Also note that many implementations have optimizations for small strings, where the string class does contain a small buffer. If the contents of the string fit in the buffer, then no memory is dynamically allocated and only the local buffer is used.

另请注意,许多实现对小字符串进行了优化,其中字符串类确实包含一个小缓冲区。如果字符串的内容适合缓冲区,则不会动态分配内存,仅使用本地缓冲区。

回答by egrunin

stringis not a simple datatype like char *. It's a class, which has implementation details that aren't necessarily visible.

string不是像char *. 它是一个class,它具有不一定可见的实现细节。

Among other things, stringincludes a counter to keep track of how big it really is.

除其他外,string包括一个计数器来跟踪它的实际大小。

char[] test = "asd";       // allocates exactly 4 bytes
string testString = "asd"; // who knows?

testString = "longer";     // allocates more if necessary

Suggestion:write a simple program and step through it using a debugger. Examine the string, and see how the private members change as the value is changed.

建议:编写一个简单的程序并使用调试器逐步完成它。检查string, 并查看私有成员如何随着值的更改而变化。

回答by Brian Roach

stringis an object, not just some memory location. It dynamically allocates memory as needed.

string是一个对象,而不仅仅是一些内存位置。它根据需要动态分配内存。

The =operator is overloaded; when you say testString = "123456789";a method is being called and deals with the const char *you passed in.

=操作符被重载; 当您说testString = "123456789";正在调用一个方法并处理const char *您传入的方法时。

回答by Tom Tanner

It's stored with a size. If you store a new string, it will optionally deallocate the existing memory and allocate new memory to cope with the change in size.

它以大小存储。如果您存储一个新字符串,它会选择性地释放现有内存并分配新内存以应对大小的变化。

And it doesn't necessarily allocate 4 bytes the first time you assign a string of 4 bytes to it. It may allocate more space than that (it won't allocate less).

并且在您第一次为其分配 4 个字节的字符串时,它不一定分配 4 个字节。它可能分配更多的空间(它不会分配更少)。