简体   繁体   English

std::vector 中的 clear() 是否会产生内存泄漏?

[英]Does clear() in std::vector generates a memory leak?

My question is pretty much an extension of this one: Is std::vector memory freed upon a clear?我的问题几乎是这个问题的扩展: std::vector 内存是否在清除时被释放?

Here people explained that the memory allocated for the elements of a std::vector is not released after calling clear().这里有人解释说,为 std::vector 的元素分配的内存在调用 clear() 后不会释放。 But will this memory be free for the OS to allow other programs to use it or will it be available for my program to use it anywhere else?但是,该内存是否可供操作系统免费使用以允许其他程序使用它,还是可供我的程序在其他任何地方使用它?

If not (if the memory continues to be allocated only for this vector and I can't access it anymore) isn't it a memory leak like the ones we have with pointers?如果不是(如果内存继续只为这个向量分配而我不能再访问它)是不是像我们用指针那样的内存泄漏? Then clear() when used alone like this would be totally unsafe right?然后像这样单独使用 clear() 是完全不安全的,对吗?

I would be happy if someone could clarify me in this one.如果有人能在这方面澄清我,我会很高兴。 Thanks.谢谢。

Here people explained that the memory allocated for the elements of a std::vector is not released after calling clear().这里有人解释说,为 std::vector 的元素分配的内存在调用 clear() 后不会释放。 But will this memory be free for the OS to allow other programs to use it or will it be available for my program to use it anywhere else?但是,该内存是否可供操作系统免费使用以允许其他程序使用它,还是可供我的程序在其他任何地方使用它?

No, the memory will still be held by the std::vector instance, and will not be available for use by the rest of the program until the vector itself is destroyed, orshrink_to_fit is called 1 .不,内存仍将由std::vector实例持有,并且在向量本身被销毁之前,程序的其余部分将无法使用,或者shrink_to_fit被称为1

If not (if the memory continues to be allocated only for this vector and I can't access it anymore) isn't it a memory leak like the ones we have with pointers?如果不是(如果内存继续只为这个向量分配而我不能再访问它)是不是像我们用指针那样的内存泄漏?

Not really.并不真地。 The memory is still released when the vector is destroyed.当vector被销毁时,内存仍然被释放。

Then clear() when used alone like this would be totally unsafe right?然后像这样单独使用 clear() 是完全不安全的,对吗?

As @user2079303 eloquently put it;正如@user2079303雄辩地说; clear isn't inherently unsafe, but the assumption that it will free memory may be. clear并不是本质上不安全的,但它会释放内存的假设可能是。

1. Potentially. 1.潜在的。 The call to shrink_to_fit is not guaranteed to free any memory.shrink_to_fit的调用不能保证释放任何内存。

Does clear() in std::vector generates a memory leak? std::vector 中的 clear() 是否会产生内存泄漏?

No.不。

But will this memory be free for the OS to allow other programs to use it or will it be available for my program to use it anywhere else?但是,该内存是否可供操作系统免费使用以允许其他程序使用它,还是可供我的程序在其他任何地方使用它?

OS can swap the memory to disk, to allow other programs to use the physical memory.操作系统可以将内存交换到磁盘,以允许其他程序使用物理内存。

if the memory continues to be allocated only for this vector and I can't access it anymore如果内存继续只为这个向量分配,我不能再访问它

You can reuse the memory by adding new objects into the vector.您可以通过向向量中添加新对象来重用内存。 Or release it by destroying the vector.或者通过破坏载体来释放它。 The destructor absolutely guarantees to free the memory.析构函数绝对保证释放内存。

isn't it a memory leak like the ones we have with pointers?这不是像我们使用指针的内存泄漏吗?

No. A memory leak happens when the pointer to the memory is lost.不会。当指向内存的指针丢失时会发生内存泄漏。 But in this case the vector safely keeps track of the pointer, and frees it when it is destroyed.但在这种情况下,向量安全地跟踪指针,并在它被销毁时释放它。

Then clear() when used alone like this would be totally unsafe right?然后像这样单独使用 clear() 是完全不安全的,对吗?

clear isn't inherently unsafe, but the assumption that it will free the memory may be. clear并不是本质上不安全的,但它会释放内存的假设可能是。

It is not a leak.这不是泄漏。 Generally speaking, a leak is when resources are allocated by your program and you lose all handles to them.一般来说,泄漏是指您的程序分配了资源并且您失去了对它们的所有句柄 An even stricter definition of a leak is when this resource allocation happens repeatedly over time.更严格的泄漏定义是这种资源分配随着时间的推移重复发生。

A simple call to clear does not fulfill that definition, because you still have access to the std::vector object after the function call;clear的简单调用不能满足该定义,因为在函数调用之后您仍然可以访问std::vector对象; the object does not simply disappear.对象不会简单地消失。 You can still call shrink_to_fit on it, or swap it with an empty vector.你仍然可以调用shrink_to_fit ,或者用一个空向量swap它。 And even that should not be necessary, because eventually, the std::vector will be destructed, and the destructor deallocates the occupied memory, or more correctly speaking, it deallocates the storage, because what happens on the OS or even hardware level is not in the scope of C++ language rules.甚至这也不应该是必要的,因为最终, std::vector将被破坏,并且析构函数释放占用的内存,或者更准确地说,它释放存储,因为操作系统甚至硬件级别上发生的事情不是在 C++ 语言规则的范围内。

If the destructor is not run because the std::vector is never destroyed due to some buggy dynamic memory handling on your part, then the existence of the std::vector object itself already causes a potential leak.如果由于某些错误的动态内存处理而导致std::vector从未被销毁而未运行析构函数,则std::vector对象本身的存在已经导致潜在的泄漏。 The problem is then not with the storage handled by the std::vector .问题不在于std::vector处理的存储。


Note that clear still causes the std::vector 's elements to be destroyed immediately.请注意, clear仍然会导致std::vector元素立即被销毁。 This has the important effect that their destructors are run.这具有运行它们的析构函数的重要效果。 So when you have, for example, a std::vector<std::string> , and you call clear on it, then all the individual std::string destructors are run, and of course that causes storage to be deallocated immediately, provided that there was actually some dynamic content handled by the strings (ie Small-String Optimisation was not used).因此,例如,当您有一个std::vector<std::string> ,并且您对其调用clear时,则运行所有单独的std::string析构函数,当然会导致立即释放存储空间,前提是实际上有一些由字符串处理的动态内容(即未使用小字符串优化)。 But it's not the storage handled by std::vector , but the storage handled by the std::string s'.但这不是std::vector处理的存储,而是std::string s' 处理的存储。

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

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