简体   繁体   English

字符串如何在c ++中分配内存?

[英]How do strings allocate memory in c++?

I know that dynamic memory has advantages over setting a fixed size array and and using a portion of it. 我知道动态内存比设置固定大小的数组并使用其中的一部分具有优势。 But in dynamic memory you have to enter the amount data that you want to store in the array. 但是在动态内存中,您必须输入要存储在阵列中的数据量。 When using strings you can type as many letters as you want(you can even use strings for numbers and then use a function to convert them). 使用字符串时,您可以根据需要键入任意数量的字母(甚至可以将字符串用于数字,然后使用函数进行转换)。 This fact makes me think that dynamic memory for character arrays is obsolete compared to strings. 这个事实使我认为,与字符串相比,字符数组的动态内存已过时。

So i wanna know what are the advantages and disadvantages when using strings? 所以我想知道使用字符串时的优缺点是什么? When is the space occupied by strings freed? 何时释放字符串占用的空间? Is maybe the option to free your dynamically allocated memory with delete an advantage over strings? 也许可以选择通过删除字符串来释放动态分配的内存,而不是删除字符串。 Please explain. 请解释。

The short answer is "no, there is no drawbacks, only advantages" with std::string over character arrays. 简短的答案是std::string比字符数组“没有,没有缺点,只有优点”。

Of course, strings do USE dynamic memory, it just hides the fact behind the scenes so you don't have to worry about it. 当然,字符串确实会使用动态内存,它只是将事实隐藏在幕后,因此您不必担心它。

In answer to you question: When is the space occupied by strings freed? 在回答您的问题时:字符串占用的空间何时释放? this post may be helpful. 这篇文章可能会有所帮助。 Basically, std::string s are freed once they go out of scope. 基本上,一旦std::string超出范围,它们就会被释放。 Often the compiler can decide when to allocate and release the memory. 通常,编译器可以决定何时分配和释放内存。

std::string usually contains an internal dynamically allocated buffer. std :: string通常包含内部动态分配的缓冲区。 When you assign data, or if you push back new data, and the current buffer size is not sufficient, a new buffer is allocated with an increased size and the old data is copied or moved to the new buffer. 当您分配数据时,或者如果您推回新数据,并且当前缓冲区大小不足,则会以增加的大小分配新缓冲区,并将旧数据复制或移动到新缓冲区中。 The old buffer is then deallocated. 然后将旧缓冲区释放。

The main buffer is deallocated when the string goes out of scope. 当字符串超出范围时,将释放主缓冲区。 If the string object is a local variable in a function (on the stack), it will deallocate at the end of the current code block. 如果字符串对象是函数(在堆栈中)中的局部变量,它将在当前代码块的末尾取消分配。 If it's a function parameter, when the function exits. 如果是函数参数,则函数退出时。 If it's a class member, whenever the class is destroyed. 如果是类成员,则每当销毁该类时。

The advantage of strings is flexibility (increases in size automatically) and safety (harder to go over the bounds of an array). 字符串的优点是灵活性(自动增加大小)和安全性(很难越过数组的边界)。 A fixed-size char array on the stack is faster as no dynamic allocation is required. 堆栈上固定大小的char数组更快,因为不需要动态分配。 But you should worry about that if you have a performance problem, and not before. 但是,如果您有性能问题,应该担心,而不是之前。

well, your question got me thinking, and then i understood that you are talking about syntax differences, because both ways are dynamic allocating char arrays. 好了,您的问题让我开始思考,然后我理解您在谈论语法差异,因为这两种方式都是动态分配char数组。 the only difference is in the need: 唯一的区别是需要:

  • if you need to create a string containing a sentence then you can, and that's fine, not to use malloc 如果您需要创建一个包含句子的字符串,那么可以,而且不要使用malloc
  • if you want an array and to "play" with it, meaning change or set the cells cording to some method, or changing it's size, then initiating it with malloc would be the appropriate way 如果您想要一个数组并“播放”它,这意味着更改或设置连接到某种方法的单元格,或更改其大小,然后使用malloc初始化它是合适的方法
  • the only reason i see to a static allocating char a[17] (for example) is for a single purpose string that you need, meaning only when you know the exact size you'll need and it won't change 我看到静态分配char a[17]的唯一原因(例如)是针对您需要的单个目的字符串,这意味着仅当您知道所需的确切大小并且它不会改变时

and one important point the i found: 我发现的一个重要点是:

In dynamic memory allocation, if the memory is being continually allocated but the one allocated for objects that are not in use, is not released, then it can lead to stack overflow condition or memory leak which is a big disadvantage. 在动态内存分配中,如果连续分配内存但未分配给未使用的对象的内存未释放,则可能导致堆栈溢出或内存泄漏,这是一个很大的缺点。

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

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