不是在动态分配的对象上调用delete总是内存泄漏? [英] Is not calling delete on a dynamically allocated object always a memory leak?

查看:145
本文介绍了不是在动态分配的对象上调用delete总是内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从讨论开始这里,我想知道以下代码是否有内存泄漏:

From the discussion started here, I'd like to know whether the following code has a memory leak:

int main()
{
   new int();
   //or
   int* x = new int();
   return 0;
}



我知道内存被操作系统回收, ?我相信它是。

I know the memory is reclaimed by the OS, but is it a leak anyway? I believe it is.

什么定义了内存泄漏?我只能在标准中找到一个参考,这不是很有帮助。

What defines a memory leak? I could only find one reference in the standard, and it wasn't very helpful.

编辑:我不想开始辩论 - 我认为...不是我想要的那种答案。

推荐答案

第二种情况不是内存泄漏。

这不是一个泄漏,因为你仍然有一个指向分配的内存的指针。

要定义内存泄漏,我想坚持定义哪些大多数内存分析工具,如valgrind使用:

Second case is not a memory leak.
It is not a leak because you still have an pointer to the memory that was allocated.
To define a memory leak I would like to stick to definition which most of memory analysis tools like valgrind use:


内存已分配,不能随后释放,因为程序不再有任何指针到分配的内存块。

Memory was allocated and cannot be subsequently freed because the program no longer has any pointers to the allocated memory block.

这篇关于不是在动态分配的对象上调用delete总是内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆