C garbage collection -crazy idea

O

onkar

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

Zealot Zuo

onkar said:
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,

Fun idea though, but I think you may try C++ auto_ptrs.
I don't think auto-free would be wrote into C standards.
 
C

Chris Dollin

onkar said:
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 ??

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

Richard Heathfield

onkar said:
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 you wouldn't be able to use it for translating any existing C
programs that correctly call free().

And how would it know when to call free()? I'll be the judge of that, thank
you.
 
K

Kohn Emil Dan

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

The question is *when* to insert the call to free(). When the function
that called malloc() returns? But in many cases memory allocated via
malloc() is used even after the function that performed the allocation
returns. So, in order to know when it is appropriate to call free(), you
need a more elaborate scheme (reference counting and tracking) which is
what a garbage collector does.

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

Garbage collectors incur a run-time penalty. Therefore most C
implementations won't use it. However, the C language by itself does not
explicitly forbid using garbage collection. In fact there are some
performant implementations of garbage collectors available.

See the following link for an implementation of a garbage collector for C
and C++.

http://www.hpl.hp.com/personal/Hans_Boehm/gc/


Emil
 
C

Chris Dollin

Richard said:
onkar said:


...then you wouldn't be able to use it for translating any existing C
programs that correctly call free().

If -- I say /if/ -- the compiler could correctly deduce where to put
the matching malloc, then it could also discard the explicit calls
to free.
And how would it know when to call free()? I'll be the judge of that, thank
you.

Verily. (For values of "verily" that mean "GC is wonderful and the
machine can do a better job of store management than I can, but C
isn't meant for the kind of programs that benefit -- if I have a GC
environment I'm sure as heck that I won't waste it on writing C
when there are lots of GC-exploiting idioms I could be using instead.")
 
G

goose

onkar said:
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 ??

How would the compiler react to this?

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

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

goose,
google for Boehm GC for C.
 
C

Chris Dollin

Chris said:
If -- I say /if/ -- the compiler could correctly deduce where to put
the matching malloc,

The matching /free/.

What idiot wrote that snippet?
 
R

Roland Pibinger

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.

Best wishes,
Roland Pibinger
 
A

af

I wouldn't say automatic placing of free() statements is a *crazy*
idea. To my point of view, this is a good idea, first. ïŠ

I believe It is quite possible to detect a lot of places where to add
free() with static code analysis. I can do it by examining code, so the
clever program also should be able to do this. It would save a lot of
$$ to issue at least a warning for such case. The only problem is
compiler developers are too busy to develop such checks, and static
code analyzers repay their high price.

I know the only free tool for static code analysis [1], and it is
unable to validate memory allocation. Probably one can improve it in
this direction. BTW, there are plenty of runtime leak detectors such as
[2].

[1] CCCC tool, http://sourceforge.net/projects/cccc
[2] Leaky, http://lxr.mozilla.org/mozilla/source/tools/leaky/leaky.html

With best regards,
Alexei

"""onkar пиÑал(а):
"""
 
R

Richard Heathfield

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

Lew Pitcher

Roland said:
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
 
C

Chris Dollin

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

Roland Pibinger

Roland Pibinger said:

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
 
R

Roland Pibinger

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
 
R

Roland Pibinger

How would the compiler react to this?

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

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

Lew Pitcher

Roland said:
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++.
 
R

Random832

2006-12-15 said:
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)
 
I

info

onkar said:
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
------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top