清除c中的效用 [英] purge like utility in c

查看:47
本文介绍了清除c中的效用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨..我们可以在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

推荐答案


ra ******************* @ gmail.com 写道:

hi ..我们可以在c中设计一个程序,它会指出任何给定c文件中可能存在的内存泄漏并修复他们......我正试图用这样的东西来b / b
但是不知道从哪里开始...


任何帮助从哪里开始会很高兴

亲切的问候

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(),例如 www。 dmalloc.com 给出,然后你运行你的程序,

幸运地让它运用所有可能的代码路径,然后你看看

在列表中仍然分配的块。


我最后一次这样做,我忘了释放一个重要的结构/>
指针因此有类似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.


使你的代码更安全的另一种方法是一件海峡夹克

宏观或功能包装,这使得它几乎不可能不需要b / b
免费的东西,例如:


#define Serious_Wrapper(var,type,size,proc)\

{type var; var = malloc(size); proc(var); free(var); }

....但当然这通常不适合您的

代码的流程。叹息。

Another way to make your code even safer is to have a strait-jacket
macro or function wrapper which makes it darn near impossible to not
free things, something like:

#define Serious_Wrapper(var,type,size,proc) \
{ type var; var = malloc( size ); proc( var ); free( var ); }
.... but of course this often doesnt fit in well with the flow of your
code. Sigh.


Ancient_Hacker写道:
Ancient_Hacker wrote:

使你的代码更安全的另一种方法是有一件海峡夹克

宏观或功能包装,这使得它几乎不可能没有免费的东西,例如:


#define Serious_Wrapper(var,type,size,proc)\

{type var; var = malloc(size); proc(var); free(var); }


...但当然这通常不适合您的

代码的流程。叹。
Another way to make your code even safer is to have a strait-jacket
macro or function wrapper which makes it darn near impossible to not
free things, something like:

#define Serious_Wrapper(var,type,size,proc) \
{ type var; var = malloc( size ); proc( var ); free( var ); }
... but of course this often doesnt fit in well with the flow of your
code. Sigh.



哦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中的效用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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