473,804 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C garbage collection -crazy idea

This idea might be vey crazy. But I hope to get answers to this .. from
comp.lang.c

If a compiler is designed such that it automatically adds a free()
matching every malloc()
then is it not a garbage collection (in the first place , garbage will
not be generated !! ) . Is it
possible to have a compiler with such feature. Or if Its not a good
idea (or may be this is an idiotic idea) please tell me why is it so ??

Thanks,

Dec 15 '06
28 2110
in**@coverageme ter.com said:

<snip>
You can allocate on the stack and not on the heap.
in this case the memory will be freed automatically
when leaving the function.

See 'man alloca'
But bear in mind that alloca() may not be available on your system. It is
not a standard C library function.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 15 '06 #21

Roland Pibinger wrote:
On 15 Dec 2006 03:59:10 -0800, "goose" wrote:
How would the compiler react to this?

int x, i;
...
int **foo = malloc (sizeof *foo * x);
...
for (i=0; i<x; i++) {
foo[i] = malloc (sizeof *foo[i]);
}
...

How many calls to free should there be, and where should
they be placed?

How would you do it manually? The compiler would roughly do the same.
It cannot; it does not have enough information at compile-time
to determine how many free() calls are needed. What if I read the
value x from a file at runtime?

goose,

Dec 15 '06 #22
"goose" <ru**@webmail.c o.zawrites:
Roland Pibinger wrote:
>On 15 Dec 2006 03:59:10 -0800, "goose" wrote:
>How would the compiler react to this?

int x, i;
...
int **foo = malloc (sizeof *foo * x);
...
for (i=0; i<x; i++) {
foo[i] = malloc (sizeof *foo[i]);
}
...

How many calls to free should there be, and where should
they be placed?

How would you do it manually? The compiler would roughly do the same.

It cannot; it does not have enough information at compile-time
to determine how many free() calls are needed. What if I read the
value x from a file at runtime?
The compiler can generate a loop as easily as you can. But I'd be
very very surprised if something like this could be implemented
usefully.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 15 '06 #23
In article <6v************ *************** ***@bt.com>,
Richard Heathfield <rj*@see.sig.in validwrote:
>If a compiler is designed such that it automatically adds a free()
matching every malloc()
>...then you wouldn't be able to use it for translating any existing C
programs that correctly call free().
It could do what existing garbage collectors for C do, and replace
free() with a no-op function. Of course, when he said the compiler
would add calls to free() he meant real_free().
>And how would it know when to call free()? I'll be the judge of that, thank
you.
If you could correctly judge it for all my programs, it would be very
helpful.

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Dec 16 '06 #24

Keith Thompson wrote:
"goose" <ru**@webmail.c o.zawrites:
Roland Pibinger wrote:
On 15 Dec 2006 03:59:10 -0800, "goose" wrote:
How would the compiler react to this?

int x, i;
...
int **foo = malloc (sizeof *foo * x);
...
for (i=0; i<x; i++) {
foo[i] = malloc (sizeof *foo[i]);
}
...

How many calls to free should there be, and where should
they be placed?

How would you do it manually? The compiler would roughly do the same.
It cannot; it does not have enough information at compile-time
to determine how many free() calls are needed. What if I read the
value x from a file at runtime?

The compiler can generate a loop as easily as you can. But I'd be
very very surprised if something like this could be implemented
usefully.
Why? alloca can be implemented in such a way that it allocates from
the "heap" not the "stack". Basically, to implement a malloca, you
just call malloc and keep a list of things you need to free along with
the point at which you need to free them. If you design your functions
to use a single exit point then you can do this yourself, you don't
even need compiler support (with the minor proviso that if a variable
goes out of scope there is a "memory leak" till the end of the
function).

- William Hughes.

- William Hughes
--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 16 '06 #25
"onkar" <on*******@gmai l.comwrote:
# This idea might be vey crazy. But I hope to get answers to this .. from
# comp.lang.c
#
# If a compiler is designed such that it automatically adds a free()
# matching every malloc()

If you want automatic garbage collection, use the auto or register
storage class.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Death is the worry of the living. The dead, like myself,
only worry about decay and necrophiliacs.
Dec 16 '06 #26
Richard Tobin said:
In article <6v************ *************** ***@bt.com>,
Richard Heathfield <rj*@see.sig.in validwrote:
<snip>
>>And how would it know when to call free()? I'll be the judge of that,
thank you.

If you could correctly judge it for all my programs, it would be very
helpful.
If the rate is right...

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 16 '06 #27

onkar wrote:
This idea might be vey crazy. But I hope to get answers to this .. from
comp.lang.c

If a compiler is designed such that it automatically adds a free()
matching every malloc()
then is it not a garbage collection (in the first place , garbage will
not be generated !! ) . Is it
possible to have a compiler with such feature. Or if Its not a good
idea (or may be this is an idiotic idea) please tell me why is it so ??

Thanks,
First of all, you might want to check out the Boehm-Demers-Weiser GC
(http://en.wikipedia.org/wiki/Boehm_GC for basic information,
http://www.hpl.hp.com/personal/Hans_Boehm/gc/ for the home page).

What you're describing isn't exactly garbage collection. GC is usually
dynamic, rather than static.

Dec 17 '06 #28
On Fri, 15 Dec 2006 13:40:18 GMT, rp*****@yahoo.c om (Roland Pibinger) wrote:
On 15 Dec 2006 05:11:33 -0800, "Lew Pitcher" wrote:
>>Roland Pibinger wrote:
>>The compiler just needs to add the 'free' call(s) in the same scope
in which *alloc is called (in inverse order).

Nope. Not a good idea.
There are many examples where the results of malloc() are or can be
passed out of scope, to the next level higher. I certainly don't want
my compiler to throw away the deliberately malloc()ed memory that my
node allocation function in my binary tree builder grabbed. It is up
to the node deallocation function to do that.

Ok, then call those functions lmalloc ('local malloc') ... and
everything is clear. You can assign locally allocated memory to local
variables and pass it to other functions but you cannot return it or
assign it to non-local variables. In a sense alloca does something
similar (but only on the stack).
This doesn't really solve the problem Lew Pitcher described.

One of the features of the current malloc()/free() model is that you can
allocate an object deeply inside the call stack, i.e. when the program's
call stack looks like:

main()
copytree()
maketree()
makenode()
malloc()

Return the pointer to the malloc'd object all the way up to copytree(),
do some other unrelated work (i.e. sort the nodes of the tree), and then
free() the allocated memory in a different place, deeply inside another
call stack:

main()
savetree()
writenode()
deletenode()
free()

By forcing all free() operations to be always in the same scope of their
respective malloc() call, you cannot use this programming model.

Dec 27 '06 #29

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

Similar topics

1
2340
by: Bob | last post by:
Are there any known applications out there used to test the performance of the .NET garbage collector over a long period of time? Basically I need an application that creates objects, uses them, and then throws them away and then monitors the garbage collection and store statistics on it, preferably in C#. I want to know what is the longest period of time that an application may lock up while garbage collection is processing. Thanks!
0
1894
by: Andrew | last post by:
When will .NET have a low-pause-time garbage collector A low-pause-time garbage collector would greatly improve .NET's ability to serve as a platform for soft real-time systems. It doesn't have to be perfect. For example, I'd be happy with something where there was at most one pause per second, each pause was less than .2 seconds, and half the process's memory was inaccessible to the application due to garbage collection management It...
11
2741
by: Rick | last post by:
Hi, My question is.. if Lisp, a 40 year old language supports garbage collection, why didn't the authors of C++ choose garbage collection for this language? Are there fundamental reasons behind this? Is it because C is generally a 'low level' language and they didn't want garbage collection to creep into C++ and ruin everything? Just wondering :)
55
4186
by: jacob navia | last post by:
Tired of chasing free(tm) bugs? Get serious about C and use lcc-win32. The garbage collector designed by Boehm is the best of its class. Very simple: #define malloc GC_malloc #define free(a) (a=NULL) NICE isn't it?
5
3620
by: Bob lazarchik | last post by:
Hello: We are considering developing a time critical system in C#. Our tool used in Semiconductor production and we need to be able to take meaurements at precise 10.0 ms intervals( 1000 measurement exactly 10 ms apart. In the future this may decrease to 5ms ). I am concerned that if garbage collection invokes during this time it may interfere with our measurement results. I have looked over the garbage collection mechanism and see no...
8
3052
by: mike2036 | last post by:
For some reason it appears that garbage collection is releasing an object that I'm still using. The object is declared in a module and instantiated within a class that is in turn instantiated by the mainline. The class that instantiated the object in question is definitely still in existence at the point garbage collection swoops in and yanks it out from under my processing. Is there a way to ensure an instantiated object cannot be freed...
28
3193
by: Goalie_Ca | last post by:
I have been reading (or at least googling) about the potential addition of optional garbage collection to C++0x. There are numerous myths and whatnot with very little detailed information. Will this work be library based or language based and will it be based on that of managed C++? Then of course there are the finer technical questions raised (especially due to pointer abuse). Is a GC for C++ just a pipe dream or is there a lot of work...
56
3722
by: Johnny E. Jensen | last post by:
Hellow I'am not sure what to think about the Garbage Collector. I have a Class OutlookObject, It have two private variables. Private Microsoft.Office.Interop.Outlook.Application _Application = null; Private Microsoft.Office.Interop.Outlook.NameSpace _Namespace = null; The Constructor: public OutlookObject()
350
11951
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use one, and in what percentage of your projects is one used? I have never used one in personal or professional C++ programming. Am I a holdover to days gone by?
158
7916
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is discouraged due to some specific reason. If someone can give inputs on the same, it will be of great help. Regards, Pushpa
0
9715
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
10352
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...
1
10354
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
9175
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
7642
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
6867
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
5673
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3835
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.