问题描述
嗨..我们可以在c中设计一个程序,它会在任何给定的c文件中指出可能的内存泄漏并修复它们....我正试图来
有这样的东西,但不知道从哪里开始......
任何关于从哪里开始的帮助都会非常感兴趣
亲切的问候
rahul
hi .. can we design a program in c which will point out the possible
memory leaks in any given c file and fix them.... i am trying to come
with something like this but do not know where to start...
any help on where to begin would be highly appriciated
kind regards
rahul
推荐答案
好吧,任何使用malloc()都不需要以后free()会泄漏....
但是我事实上,理论上不可能弄清楚mallocs()
从不执行free()而不执行所有可能的路径通过代码
所有可能的输入。
最好的方法是编写真正结构化的代码,例如:
p = malloc(...);
dosomethingwith(p);
免费(p);
....并且没有任何其他路径可以绕过免费()。
在实践中,通常做的是你写的非常糟糕的意大利面条
代码,而free()在malloc()附近无处可寻然后使用日志记录
malloc(),例如给出,然后你运行你的程序,
幸运地让它运用所有可能的代码路径,然后你看看
在列表中仍然分配的块。
我最后一次这样做,我忘了释放一个重要的结构/>
指针因此有类似36,337个不同指针使用
124,543,000字节。哎哟。你应该首先在
中编写好的代码。
well, any use of malloc() without a later free() is going to leak....
But I think it''s ttheoretically impossible to figure out what mallocs()
never do a free() without executing all possible paths thru the code
with all possible inputs.
The best way is to write really structured code, something like this:
p = malloc( ... );
dosomethingwith(p);
free( p );
.... and don''t have any other paths that can bypass the free().
In practice what''s usually done is you write really crappy spaghetti
code, with the free()''s nowhere near the malloc()''s, then use a logging
malloc(), such as www.dmalloc.com gives out, then you run your program,
with luck having it exercise all the possible code paths, then you look
at the list of still-allocated blocks.
The last time I did so, I forgot to free one important struct of
pointers so there were something like 36,337 unfreed pointers using
124,543,000 bytes. Ouch. You should really just write good code in
the first place.
哦BCPL's aptovec的快乐时光。
-
Chris" ;埃森-9和计数 Dollin
快捷方式都是人们使用它们。
Oh happy days of BCPL''s aptovec.
--
Chris "Essen -9 and counting" Dollin
The shortcuts are all full of people using them.
这篇关于清除c中的效用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!