473,804 Members | 2,191 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 2111
Roland Pibinger said:
On Fri, 15 Dec 2006 11:36:33 +0000, Chris Dollin wrote:
>>onkar wrote:
>>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 ??

How is the compiler supposed to know /where/ to add the `free` call?

The compiler just needs to add the 'free' call(s) in the same scope in
which *alloc is called (in inverse order).When a pointer to allocated
memory is returned from the scope no call to free has to be performed.
Doesn't that kind of defeat the purpose? It certainly would for me, since
most of my pointers-to-allocated-memory end up getting returned by the
function in which they are defined.

--
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 #11

Roland Pibinger wrote:
On Fri, 15 Dec 2006 11:36:33 +0000, Chris Dollin wrote:
onkar wrote:
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 ??
How is the compiler supposed to know /where/ to add the `free` call?

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.
When a pointer to allocated
memory is returned from the scope no call to free has to be performed.
/If/ I grant you this, then you have not only "improved" the garbage
collection, you have crippled the ability to use dynamically allocated
memory in conjunction with function calls. Sorry, but I believe that
the freedom I already have wrt function calls is not worth trading away
in that manner.
I guess that would not be difficult to implement
You might be surprised at how difficult this is to implement
/correctly/.
but is not compatible to current C
Or past C, or (if I understand correctly) future C, either
and therefore unrealistic.
--
Lew

Dec 15 '06 #12
Roland Pibinger wrote:
On Fri, 15 Dec 2006 11:36:33 +0000, Chris Dollin wrote:
>>onkar wrote:
>>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 ??

How is the compiler supposed to know /where/ to add the `free` call?

The compiler just needs to add the 'free' call(s) in the same scope in
which *alloc is called (in inverse order). When a pointer to allocated
memory is returned from the scope no call to free has to be performed.
I guess that would not be difficult to implement but is not compatible
to current C and therefore unrealistic.
Not compatible with being useful, either.

--
Chris "Perikles triumphant" Dollin
A rock is not a fact. A rock is a rock.

Dec 15 '06 #13
On Fri, 15 Dec 2006 13:10:24 +0000, Richard Heathfield wrote:
>Roland Pibinger said:
>The compiler just needs to add the 'free' call(s) in the same scope in
which *alloc is called (in inverse order).When a pointer to allocated
memory is returned from the scope no call to free has to be performed.

Doesn't that kind of defeat the purpose? It certainly would for me, since
most of my pointers-to-allocated-memory end up getting returned by the
function in which they are defined.
Not necessarily most. It depends on the programming style. You use
'bottom-up' I prefer 'top-down'. Even if some allocated memory is
returned from the function especially string manipulation would
benefit from a 'local' alloc (which should have names different from
the known *alloc functions, e.g. lmalloc).

Best regards,
Roland Pibinger
Dec 15 '06 #14
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).

Best wishes,
Roland Pibinger
Dec 15 '06 #15
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.
Dec 15 '06 #16

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).
First off, I don't see the difference between your proposed lmalloc()
function and "automatic storage", other than your proposed lmalloc()
needs new compiler magic, where "automatic storage" does not.

Consider

{
struct a { int aa; char ab;};
struct a element, *ptr_to_element ; /* "automatic storage" */
ptr_to_element = &element;

/* do useful work with element, ptr_to_element */

} /* element, ptr_to_element automatically deallocated */

vs

{
struct a { int aa; char ab;};
struct a *ptr_to_element ;

ptr_to_element = lmalloc(sizeof *ptr_to_element );

/* do useful work with ptr_to_element */

} /* ptr_to_elementt automagically deallocated */

Of course, "automatic storage" doesn't need to be checked for
successful allocation, where your lmalloc() does. OTOH, "automatic
storage" only allocates the object and you have to derive a pointer in
code, where your lmalloc() only provides the pointer, which you must
use in all later work.

To me, your lmalloc() proposal is a half-measure. If you are going to
change the compiler and the language to perform an automatic garbage
collection, you might as well make it work properly and not require a
function call to allocate something that will not (and must not) be
deallocated by a function call. So, why not just add a new language
element instead of your lmalloc()? How about

{
struct a { int aa; char ab;};
new struct a *ptr_to_element ; /* keyword "new" takes the place of
lmalloc() */

/* do useful work with ptr_to_element */

} /* ptr_to_elementt automagically deallocated */

Of course, now we've made the complete tranformation to a new language
that looks like C++.

Dec 15 '06 #17
2006-12-15 <45************ ***@news.utanet .at>,
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).
alloca is not a standard function. Also, GNU "libiberty" contains
a version of alloca that uses "the heap" (i.e. it calls malloc and
automatically frees stuff later)
Dec 15 '06 #18
2006-12-15 <11************ **********@f1g2 000cwa.googlegr oups.com>,
af@ipclearingbo ard.com wrote:
BTW, there are plenty of runtime leak detectors such as [2].

[2] Leaky, http://lxr.mozilla.org/mozilla/sourc...aky/leaky.html
Or valgrind, which is, as far as I can tell, a bit more well-known, but
doesn't specifically support xpcom.
Dec 15 '06 #19
onkar <on*******@gmai l.comwrote:
Newsgroups: comp.lang.c

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,
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'

------------------------------------------
CoverageMeter Software Factory
Code Coverage Tool for C/C++ <
Homepage: http://www.coveragemeter.com
------------------------------------------
Dec 15 '06 #20

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
10603
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
10356
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
10099
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...
1
7643
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
6869
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();...
1
4314
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
3836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3003
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.