473,836 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

purge like utility in c

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

Oct 10 '06 #1
118 4438

ra************* ******@gmail.co m 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.

Oct 10 '06 #2
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.

Oct 10 '06 #3
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.

Oct 10 '06 #4
Ancient_Hacker wrote:
ra************* ******@gmail.co m 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
es*****@acm-dot-org.invalid
Oct 10 '06 #5
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
Oct 10 '06 #6
In article <45************ *@news.utanet.a t>,
Roland Pibinger <rp*****@yahoo. comwrote:
>BTW, the Standard C library
doesn't allocate objects that have to be freed by the caller.
malloc() does...

-- Richard
Oct 10 '06 #7
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.

Oct 10 '06 #8
ra************* ******@gmail.co m 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
Oct 10 '06 #9
jacob navia wrote:
ra************* ******@gmail.co m 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/

Oct 10 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4419
by: Dan Caron | last post by:
I have to create a stored procedure to purge "x" # of records from a table. I have two tables (script below): Schedule ScheduleHistory I need to purge records out of ScheduleHistory. The problem is that the # of records that needs to be "kept" is dynamic, and stored in the Schedule table. So all records in ScheduleHistory should be purged, except for the most recent "x" number of records.
1
2276
by: Heather | last post by:
Is it possible to automatically purge records after a six month period in Microsoft Access 97?
5
2306
by: Diffident | last post by:
Hello All, I have written a webform which is by default derived from "Page" class. I have coded another utility class with few methods and an object of this class is instantiated from the webfom class. Methods inside the utility class set few global variables which act as mediums of persistence to hold data. Now my question is all works fine during my initial request and all the global variables are set correctly, but during later...
5
5408
by: Jeff | last post by:
We are using .Net and the wsdl Utility to generate proxies to consume web services built using the BEA toolset. The data architects on the BEA side create XML schemas with various entities in separate files for ease of maintainability. These schemas are all part of the same namespace. When defining a web service that access more than one of these entities, the wsdl file generated by BEA contains multiple schema elements with the same...
1
5965
by: huyuhui | last post by:
The following is a question of LOAD utility. Question: How does the DB2 enforce table check constraints for data added to table with the LOAD utility? A. With the BUILD phase of LOAD B. With the SET INTEGRITY statement C. With the DELETE phase of the LOAD D. With the UPDATE CONSTRAINTS statement Answer is A
27
2160
by: Neil | last post by:
Anyone have a recommendation for a good inexpensive or free utility for developing table schemas.
3
2057
by: kababoom | last post by:
Hi All, I am new to PLSQL. I am posting this query. Wat i need : to purge and export oracle table data which is more than 1 month old onto a csv file Thks for any advice :)
1
1476
by: kenderkin17 | last post by:
I have an older Database of personel records that I need to completely purge, but keep all of the current macros data and relationships. Is there a command line or code that will simply remove all of the stored data in the input fields for the DB while maintaining the integrity of the previous commands and macros? I understand that you may need to know all of the previous macros and things of that nature. If there is a simple command that...
0
2036
by: hsheboul | last post by:
Thanks to Markus for pointing me to all the Apache stuff http://bytes.com/topic/html-css/answers/876342-automating-web-site-template My Web site is hosted on a shared Linux hosting, that runs the Apache 2.2 I got multiple static "utility" files: CSS, JavaScripts, Impages, etc. In order to maintain a coherent and consistent structure, I put all these kind of utility files in one directory hierarchy. However, this directory structure might...
0
9816
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9668
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10840
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10546
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9371
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7790
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6978
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
3112
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.