473,883 Members | 1,690 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
118 4441
On Tue, 10 Oct 2006 13:13:21 -0400, James Antill wrote:
>On Tue, 10 Oct 2006 14:19:12 +0000, Roland Pibinger wrote:
>Symmetric resource acquisition
and release is the most powerful resource management pattern in C.

Define "most powerful". It's certainly not the most useful in many cases.
And the better idiom for resource management is?
>And for many "types" pre-allocating is very much not the safest option
(like string types).
pre-allocating? It's merely about the symmetry of allocation and
deallocation (eg. malloc/free) in one scope.
>It also strongly ties the caller to the callee
implicitly, which you often don't want from a design/quality POV.
The ties are explicit and the responsibilitie s are clear: Whoever
allocates a resource has to deallocate/release it in the same scope.
> BTW, the Standard C library
doesn't allocate objects that have to be freed by the caller.

Well apart from *alloc, there are at least two in the Std. library: fopen
and tmpfile. Both due to not wanting the callee to have to know intimately
about the object created. If you then get out of the theoretical there
are many more examples like: asprintf, acl_get_file and opendir.
The point is that you allocate a resource (with malloc, fopen,
opendir, ...) which you symmetrically deallocate/release (with free,
fclose, closedir, ...).

Best regards,
Roland Pibinger
Oct 10 '06 #31
jacob navia said:
Flash Gordon wrote:
>>
<snip>
>>
Stop talking complete rubbish. As you well know valgrind can be used
with any standard C program where as garbage collection cannot.

Who is talking complete rubbish?

Here is a list of limitations of valgrind:
....none of which is relevant to the fact that valgrind is an external
utility, like gdb or time or gprof or cat or type. It isn't part of the
program. You don't render your program non-portable by using it. Unlike
your garbage collection thing.

<snip>

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 10 '06 #32
jacob navia said:

<snip>
>
You do:
#define malloc(a) GC_malloc(a)
#define free(a)

And that is it.
foo.c:117: warning: implicit declaration of function `GC_malloc'
foo.c:117: warning: initialization makes pointer from integer without a cas
foo.c:117: undefined reference to `GC_malloc'
collect2: ld returned 1 exit status
make: *** [foo] Error 1

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 10 '06 #33
jacob navia posted:
#define malloc(a) GC_malloc(a)

Two caveats:

int main(void)
{
void *(*p)(size_t) = malloc;

(malloc)(56);

return 0;
}
--

Frederick Gotham
Oct 10 '06 #34
Richard Heathfield wrote:
jacob navia said:

<snip>
>>You do:
#define malloc(a) GC_malloc(a)
#define free(a)

And that is it.


foo.c:117: warning: implicit declaration of function `GC_malloc'
foo.c:117: warning: initialization makes pointer from integer without a cas
foo.c:117: undefined reference to `GC_malloc'
collect2: ld returned 1 exit status
make: *** [foo] Error 1
Yes, do not use GC_malloc. You see?
It doesn't work.
Stay with "free" and "malloc" Heathfield. GC_malloc is too much for you.
Oct 10 '06 #35
Richard Heathfield wrote:
jacob navia said:

>>Flash Gordon wrote:
<snip>
>>>Stop talking complete rubbish. As you well know valgrind can be used
with any standard C program where as garbage collection cannot.

Who is talking complete rubbish?

Here is a list of limitations of valgrind:


...none of which is relevant to the fact that valgrind is an external
utility, like gdb or time or gprof or cat or type. It isn't part of the
program. You don't render your program non-portable by using it. Unlike
your garbage collection thing.

<snip>
Boehm's GC runs under linux, windows, hp unices, and *many* OS.

But I do not want that YOU use a GC Heathfield, since it is
too much for you. Stay with your malloc/free.

Besides, if you use the GC you will not be able to run in the embedded
OS in your toaster.

WHO CARES?
Oct 10 '06 #36
Frederick Gotham wrote:
Richard Heathfield posted:

>>>The garbage collector can be used as an external library.

Third-party libraries are not topical here.

I suppose the most basic form would be something like:

#include <stddef.h>
#include <stdlib.h>

typedef struct AllocLink {
void *p;
struct AllocLink *p_prev;
} AllocLink;

AllocLink first_link = {0,0};
AllocLink *plink = &first_link;

void *mallocGC(size_ t const len)
{
void freeGC(void);

static int first_time = 1;
AllocLink *const p_new_link = malloc(sizeof *p_new_link);

if (!p_new_link) return 0;

p_new_link->p = malloc(len);

if(!p_new_link->p) { free(p_new_link ); return 0; }

p_new_link->p_prev = plink;

plink = p_new_link;

if(first_time) first_time = 0, atexit(freeGC);

return plink->p;
}

void freeGC(void)
{
AllocLink *p_prev;

do
{
free(plink->p);
p_prev = plink->p_prev;
free(plink);
plink = p_prev;
} while(plink->p);
}
The behaviour would be undefined if you were to use "free" to deallocate
memory allocated by "mallocGC".
Maybe. Better do not use it.

Use malloc/free and do everything manually with all the associated bugs.

I was recommending the GC for other people that like caring about their
programs, their algorithms, their deadlines, instead of chasing
malloc/free bugs!!!

You like malloc/free?

OK. I do not earn any percentages in GC sales!
Oct 10 '06 #37
Richard Heathfield wrote:
jacob navia said:

<snip>

>>Look, seeing the pain in the... of malloc/free,


Of all the things in C that can cause pain, this is one of the most trivial.
It is fairly easy not to leak memory, and remarkably easy to detect memory
leaks. If you can't do it, fine, but that's your problem, nobody else's.
Incredible.

There is a whole INDUSTRY of software for chasing
malloc/free bugs. Huge software systems like valgrind,
purify, etc etc are built JUST TO CHASE THOSE BUGS.

Incredible, it is SO EASY to avoid any problems and
there are SO MANY people trying to fix that!!!

They are all stupid, like me... They did not see what
Heathfield sees... Damm it!

Oct 10 '06 #38
jacob navia said:
Richard Heathfield wrote:
>jacob navia said:

<snip>
>>>You do:
#define malloc(a) GC_malloc(a)
#define free(a)

And that is it.


foo.c:117: warning: implicit declaration of function `GC_malloc'
foo.c:117: warning: initialization makes pointer from integer without a
cas foo.c:117: undefined reference to `GC_malloc'
collect2: ld returned 1 exit status
make: *** [foo] Error 1

Yes, do not use GC_malloc. You see?
It doesn't work.
Precisely so.
Stay with "free" and "malloc" Heathfield. GC_malloc is too much for you.
On the contrary, it's not enough.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 10 '06 #39
jacob navia said:

<snip>
Boehm's GC runs under linux, windows, hp unices, and *many* OS.
That's funny. I tried it under Linux and it didn't even exist, let alone
run.

<snip>
WHO CARES?
You don't, obviously. But comp.lang.c is for discussions about C, not
discussions about stuff that isn't C.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Oct 10 '06 #40

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

Similar topics

1
4423
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
2277
by: Heather | last post by:
Is it possible to automatically purge records after a six month period in Microsoft Access 97?
5
2310
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
5966
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
2165
by: Neil | last post by:
Anyone have a recommendation for a good inexpensive or free utility for developing table schemas.
3
2059
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
1477
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
2037
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
9933
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
11123
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...
1
10836
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10407
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
9567
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
7960
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
7114
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();...
0
5794
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5982
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.