Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > purge like utility in c

Reply
Thread Tools

purge like utility in c

 
 
ramasubramanian.rahul@gmail.com
Guest
Posts: n/a
 
      10-10-2006
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

 
Reply With Quote
 
 
 
 
Ancient_Hacker
Guest
Posts: n/a
 
      10-10-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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


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.

 
Reply With Quote
 
 
 
 
Ancient_Hacker
Guest
Posts: n/a
 
      10-10-2006
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.

 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      10-10-2006
Ancient_Hacker wrote:

> 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.


Oh happy days of BCPL's aptovec.

--
Chris "Essen -9 and counting" Dollin
The shortcuts are all full of people using them.

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      10-10-2006
Ancient_Hacker wrote:

> (E-Mail Removed) wrote:
>
>>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

>
>
> 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().


A drawback of this advice is that it ties the lifetime of
the dynamic variable to a lexical scope of the code. That is,
it removes most of the reason for having dynamic storage in
the first place; the only advantage over plain `auto' variables
would be the (possibly) less stringent size restrictions.

The chief reason for using dynamic memory is to create
objects whose lifetime is independent of the execution context.
I want to be able to malloc() something in a function, add the
something to a hash table or splay tree or some such, and return
from the function with the something still intact. If I'm not
allowed to do that, there's little point in having malloc().

--
Eric Sosman
(E-Mail Removed)lid
 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      10-10-2006
On Tue, 10 Oct 2006 08:00:44 -0400, Eric Sosman wrote:
> A drawback of this advice is that it ties the lifetime of
>the dynamic variable to a lexical scope of the code.


That's a feature not a drawback!

>That is,
>it removes most of the reason for having dynamic storage in
>the first place; the only advantage over plain `auto' variables
>would be the (possibly) less stringent size restrictions.


.... which really is a huge advantage. Symmetric resource acquisition
and release is the most powerful resource management pattern in C.

> The chief reason for using dynamic memory is to create
>objects whose lifetime is independent of the execution context.


That's no contradiction to symmetric resource management.

>I want to be able to malloc() something in a function, add the
>something to a hash table or splay tree or some such, and return
>from the function with the something still intact. If I'm not
>allowed to do that, there's little point in having malloc().


If you feel the need to return a dynamically allocated object just
allocate it one scope higher and pass it to the function thereby
preserving symmetry of de-/allocation. BTW, the Standard C library
doesn't allocate objects that have to be freed by the caller.

Best regards,
Roland Pibinger
 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      10-10-2006
In article <(E-Mail Removed)>,
Roland Pibinger <(E-Mail Removed)> wrote:
>BTW, the Standard C library
>doesn't allocate objects that have to be freed by the caller.


malloc() does...

-- Richard
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      10-10-2006
Roland Pibinger wrote:

> If you feel the need to return a dynamically allocated object just
> allocate it one scope higher and pass it to the function thereby
> preserving symmetry of de-/allocation.


Not obviously possible in general. For example, a parser that delivers
an abstract syntax tree: you don't know what to allocate until you
come to allocate it.

(Also, detaching allocation from initialisation is an open invitation
to misuse the consequent mutability of the tree objects.)

--
Chris "Essen -9 and counting" Dollin
The shortcuts are all full of people using them.

 
Reply With Quote
 
jacob navia
Guest
Posts: n/a
 
      10-10-2006
(E-Mail Removed) wrote:
> 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
>



Stop messing with malloc/free and use a garbage collector.
The machine will do the hard work, you do the driving.

jacob
 
Reply With Quote
 
Chris Dollin
Guest
Posts: n/a
 
      10-10-2006
jacob navia wrote:

> (E-Mail Removed) wrote:
>> 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

> Stop messing with malloc/free and use a garbage collector.
> The machine will do the hard work, you do the driving.


Be aware that if you do so you are leaving Standard C. That
may be a reasonable choice for generic-you, or it may not.
Make sure generic-you understand the consequences.

If one is prepared to give up C standardness for garbage collection,
there are other languages Out There that come with GC as standard.
Some of them are even designed for it.

--
Chris "Essen -9 and counting" Dollin
"No-one here is exactly what he appears." G'kar, /Babylon 5/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Utility to format/purge camera memory? efishta Digital Photography 1 04-16-2004 09:29 AM
purge in mozilla VC Firefox 3 02-26-2004 09:08 AM
Diable Explorer Button or Purge History List =?Utf-8?B?RGF2aWQ=?= ASP .Net 2 02-19-2004 06:36 PM
Re: Microsoft win2k and XP , hidden & Temp files and such. Cache, URLS, ERASE , PURGE kraken Computer Security 4 10-11-2003 03:28 PM
Re: Microsoft win2k and XP , hidden & Temp files and such. Cache, URLS, ERASE , PURGE erniegalts Computer Security 0 08-19-2003 03:51 AM



Advertisments