在C,C ++内存泄漏;忘了做自由,删除 [英] Memory leak in C,C++; forgot to do free,delete

查看:130
本文介绍了在C,C ++内存泄漏;忘了做自由,删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们使用malloc和C ++中使用新的内存用C分配。我知道,内存分配必须的
被释放或使用C中的自由,在删除C ++还给操作系统。
分配内存后,如果我忘了免费使用/删除,这意味着将有内存泄漏。

We allocate memory in C using malloc and in C++ using new. I know that memory allocated must be freed or given back to OS using free in C and delete in C++. If I forgot to use free/delete after allocating memory, it means there will be memory leak.

现在,我的问题是,只有在程序执行的时间周期此内存泄漏;或者是永久的泄漏/灭失或一旦我重新启动系统再次得到了什么?什么是内部过程实际上?什么是内存泄漏/损失意味着什么?

Now, my question is, is this memory leak only during the time period of execution of program; or is it permanent leak/loss or is it gained again once I restart the system ? What is the internal process actually ? What does memory leak/loss mean exactly?

我将非常感激,如果有人可以解释这个细节还是给我提供了一些不错的参考。

I would be very grateful if someone could explain this in detail or provide me with some nice references.

更新1

阅读一些答案后,我才知道,内存返还给OS /系统程序结束后,如果是这样的话,为什么每个人都需要那么在乎内存泄漏,为什么preventing内存泄漏是非常重要的?

After reading some answers, I learnt that memory is given back to OS/system after program terminates, if that is so, why everyone needs to care so much about memory leak, and why preventing memory leak is very important ?

更新2

所以,内存泄漏应pvented $ P $,以使系统不崩溃,由于缺少足够的内存分配的目的?

So, memory leak should be prevented so that the system doesn't crash due to lack of sufficient memory for allocation purposes ??

更新3

所以,看完所有的答案后,我意识到,内存泄漏是prevent系统崩溃安静的重要问题。但是,对于像我这样的初学者,我怎么能肯定,如果我的计划是从内存泄漏完全免费的。我尝试做免费,删除,如果我使用malloc,新的,但有时,它就会变得混乱。有没有我可以用它来知道我的计划是做任何内存泄漏的工具或方法?

So, after reading all answers, I realized that memory leak is quiet important issues to prevent system crashes. But, for a beginner like me, how can I be sure if my program is completely free from memory leak. I try to do free,delete if I am using malloc,new but sometimes, it gets messy. Is there any tool or method which I can use to know if my program is doing any memory leak ?

更新4

阅读的答案后,我现在明白了内存泄漏免费code,较少使用新/删除,多使用STL的重要性,学到了新的东西像RAII,Valgrind的和良好的编程习惯。
感谢所有:)

After reading answers, I have now understood the importance of memory leak free code, less use of new/delete, more use of STL, learnt new stuffs like RAII,valgrind and good programming practices. Thanks to all :)

推荐答案

这是每个进程的。一旦你的进程退出,分配的内存返回到操作系统,以供其他程序使用(新的或现有的)。

It's per-process. Once your process exits, the allocated memory is returned to the OS for use by other processes (new or existing).

要回答你的问题,编辑,有一个在你的机器内存只有有限数量。所以,如果你有内存泄漏,那么主要问题是内存不可用于的其他的流程使用。次要,但不可忽略的,效果是你的过程映像的增长,你会换到光盘和性能将受到打击。最后,你的程序将耗尽所有内存在系统中失败,因为它无法分配任何内存本身。

To answer your edited question, there's only a finite amount of memory in your machine. So if you have a memory leak, then the major problem is that the memory isn't available for other processes to use. A secondary, but not negligible, effect is that your process image grows, you'll swap to disc and performance will be hit. Finally your program will exhaust all the memory in the system and fail, since it's unable to allocate any memory for itself.

这是值得商榷的,对于具有寿命短的小程序,内存泄漏是可以容忍的,因为泄漏的内存将是数量少而短暂。

It's arguable that for a small process with a short lifetime, memory leaks are tolerable, since the leaked memory will be small in quantity and short-lived.

看看这个资源,对于可能更多信息比你所需要的。我们现在讨论的是这里的动态的分配。

Take a look at this resource, for possibly more info than you'll ever need. What we're discussing here is dynamic or heap allocation.

这篇关于在C,C ++内存泄漏;忘了做自由,删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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