std::vector 中的 clear() 是否会产生内存泄漏?
Does clear() in std::vector generates a memory leak?
我的问题几乎是这个问题的延伸:Is std::vector memory freed upon a clear?
这里人家解释说,astd::vector的元素分配的内存在调用clear()后并没有释放。但是这个内存是否会免费供 OS 允许其他程序使用它,或者它是否可供我的程序在其他任何地方使用它?
如果不是(如果继续只为这个vector分配内存,我就不能再访问它了)是不是像指针那样的内存泄漏?那么单独使用 clear() 就完全不安全了吧?
如果有人能在这方面澄清我的话,我会很高兴。谢谢
Here people explained that the memory allocated for the elements of a std::vector is not released after calling 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?
不,内存仍将由 std::vector
实例持有,并且在 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?
不是真的。 vector销毁的时候内存还是会释放的
Then clear() when used alone like this would be totally unsafe right?
正如 @user2079303 雄辩地说的那样; clear
本身并不是不安全的,但它会释放内存的假设可能是。
1. 有可能。调用 shrink_to_fit
不能保证释放任何内存。
Does clear() in std::vector generates a memory leak?
没有
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可以将内存交换到磁盘,让其他程序使用物理内存。
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?
没有。当指向内存的指针丢失时,就会发生内存泄漏。但是在这种情况下,vector 会安全地跟踪指针,并在它被销毁时释放它。
Then clear() when used alone like this would be totally unsafe right?
clear
本身并不是不安全的,但它会释放内存的假设可能是不安全的。
这不是泄漏。一般来说,泄漏是指您的程序分配了资源,而您 失去了它们的所有句柄 。更严格的泄漏定义是这种资源分配随时间重复发生。
对 clear
的简单调用不满足该定义,因为在函数调用后您仍然可以访问 std::vector
对象;该对象不会简单地消失。您仍然可以在其上调用 shrink_to_fit
,或使用空向量调用 swap
。甚至那也不是必需的,因为最终 std::vector
将被破坏,并且 析构函数 释放占用的内存,或者更正确地说,它释放存储,因为OS 甚至硬件级别发生的事情不在 C++ 语言规则的范围内。
如果析构函数是 not 运行 因为 std::vector
永远不会因为您的动态内存处理错误而被销毁,那么存在std::vector
对象本身已经导致了潜在的泄漏。问题不在于 std::vector
.
处理的存储
请注意,clear
仍然会导致 std::vector
的 元素 立即被销毁。这具有 他们的 析构函数 运行 的重要作用。因此,例如,当您有一个 std::vector<std::string>
,并在其上调用 clear
,那么所有单独的 std::string
析构函数都是 运行,当然还有 that 会立即释放存储空间,前提是字符串实际上处理了一些动态内容(即未使用小字符串优化)。但这不是 std::vector
处理的存储,而是 std::string
s 处理的存储。
我的问题几乎是这个问题的延伸:Is std::vector memory freed upon a clear?
这里人家解释说,astd::vector的元素分配的内存在调用clear()后并没有释放。但是这个内存是否会免费供 OS 允许其他程序使用它,或者它是否可供我的程序在其他任何地方使用它?
如果不是(如果继续只为这个vector分配内存,我就不能再访问它了)是不是像指针那样的内存泄漏?那么单独使用 clear() 就完全不安全了吧?
如果有人能在这方面澄清我的话,我会很高兴。谢谢
Here people explained that the memory allocated for the elements of a std::vector is not released after calling 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?
不,内存仍将由 std::vector
实例持有,并且在 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?
不是真的。 vector销毁的时候内存还是会释放的
Then clear() when used alone like this would be totally unsafe right?
正如 @user2079303 雄辩地说的那样; clear
本身并不是不安全的,但它会释放内存的假设可能是。
1. 有可能。调用 shrink_to_fit
不能保证释放任何内存。
Does clear() in std::vector generates a memory leak?
没有
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可以将内存交换到磁盘,让其他程序使用物理内存。
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?
没有。当指向内存的指针丢失时,就会发生内存泄漏。但是在这种情况下,vector 会安全地跟踪指针,并在它被销毁时释放它。
Then clear() when used alone like this would be totally unsafe right?
clear
本身并不是不安全的,但它会释放内存的假设可能是不安全的。
这不是泄漏。一般来说,泄漏是指您的程序分配了资源,而您 失去了它们的所有句柄 。更严格的泄漏定义是这种资源分配随时间重复发生。
对 clear
的简单调用不满足该定义,因为在函数调用后您仍然可以访问 std::vector
对象;该对象不会简单地消失。您仍然可以在其上调用 shrink_to_fit
,或使用空向量调用 swap
。甚至那也不是必需的,因为最终 std::vector
将被破坏,并且 析构函数 释放占用的内存,或者更正确地说,它释放存储,因为OS 甚至硬件级别发生的事情不在 C++ 语言规则的范围内。
如果析构函数是 not 运行 因为 std::vector
永远不会因为您的动态内存处理错误而被销毁,那么存在std::vector
对象本身已经导致了潜在的泄漏。问题不在于 std::vector
.
请注意,clear
仍然会导致 std::vector
的 元素 立即被销毁。这具有 他们的 析构函数 运行 的重要作用。因此,例如,当您有一个 std::vector<std::string>
,并在其上调用 clear
,那么所有单独的 std::string
析构函数都是 运行,当然还有 that 会立即释放存储空间,前提是字符串实际上处理了一些动态内容(即未使用小字符串优化)。但这不是 std::vector
处理的存储,而是 std::string
s 处理的存储。