Isn't there anything like garbage collector in C?

X

xicloid

I'm trying to write a program that saves numbers without any
duplications.
I was thinking I could start by storing the first number in an array
with size one,
and if the next number is not in the array I've created, I save the
first number and the new input number
in a new array with size two and delete the first array from memory.
This goes on until total of twenty numbers are input into the program.

But is it possible to delete things from memory yourself?
Isn't there anything like garbage collector in C?
 
P

Philip Potter

I'm trying to write a program that saves numbers without any
duplications.
I was thinking I could start by storing the first number in an array
with size one,
and if the next number is not in the array I've created, I save the
first number and the new input number
in a new array with size two and delete the first array from memory.
This goes on until total of twenty numbers are input into the program.

But is it possible to delete things from memory yourself?
Isn't there anything like garbage collector in C?

This sounds like a few different questions.

Yes, you can delete things from memory yourself, but only if you created
them yourself first. Look up malloc() and free() [and possibly also
realloc()] in your textbook.

No, there is no garbage collector in standard C. Just to be clear that
we are talking about the same thing, a garbage collector *automatically*
cleans up memory which you have allocated once you have stopped using it
-- although its idea of "once you have stopped using it" can be
different from yours, and often errs on the side of keeping things
allocated a bit longer than necessary.

As for your program design, if you know that you are going to input
twenty numbers, why don't you allocate space for twenty numbers to begin
with? Use another variable to store how many numbers have been input so far.
 
K

Keith Thompson

I'm trying to write a program that saves numbers without any
duplications.
I was thinking I could start by storing the first number in an array
with size one,
and if the next number is not in the array I've created, I save the
first number and the new input number
in a new array with size two and delete the first array from memory.
This goes on until total of twenty numbers are input into the program.

But is it possible to delete things from memory yourself?

Use malloc to allocate memory, realloc to change the size of an
allocated block, free to deallocate it, and your textbook or system
documentation to find out how to use these functions.

Incidentally, your method has to re-scan the entire array for each
input number. There are ways to do this more efficiently -- but if
you're only reading 20 numbers, the difference is unlikely to be
noticeable.
Isn't there anything like garbage collector in C?

Standard C has no *automatic* garbage collection.
 
C

Christopher Benson-Manica

[comp.lang.c] Keith Thompson said:
Standard C has no *automatic* garbage collection.

I would not say that free() constitutes "manual" garbage collection
either, although that may be splitting a meaningless semantic hair.
 
R

Richard

Christopher Benson-Manica said:
[comp.lang.c] Keith Thompson said:
Standard C has no *automatic* garbage collection.

I would not say that free() constitutes "manual" garbage collection
either, although that may be splitting a meaningless semantic hair.

What on earth would it be if not manual?

You must explicitly call it.
 
S

santosh

Richard said:
Christopher Benson-Manica said:
[comp.lang.c] Keith Thompson said:
Standard C has no *automatic* garbage collection.

I would not say that free() constitutes "manual" garbage collection
either, although that may be splitting a meaningless semantic hair.

What on earth would it be if not manual?

You must explicitly call it.

Actually C does have automatic memory management. You just have to use
auto objects. :) Whether this constitutes garbage collection is
probably just pedantry.
 
X

xicloid

I'm trying to write a program that saves numbers without any
duplications.
I was thinking I could start by storing the first number in an array
with size one,
and if the next number is not in the array I've created, I save the
first number and the new input number
in a new array with size two and delete the first array from memory.
This goes on until total of twenty numbers are input into the program.
But is it possible to delete things from memory yourself?
Isn't there anything like garbage collector in C?

This sounds like a few different questions.

Yes, you can delete things from memory yourself, but only if you created
them yourself first. Look up malloc() and free() [and possibly also
realloc()] in your textbook.

No, there is no garbage collector in standard C. Just to be clear that
we are talking about the same thing, a garbage collector *automatically*
cleans up memory which you have allocated once you have stopped using it
-- although its idea of "once you have stopped using it" can be
different from yours, and often errs on the side of keeping things
allocated a bit longer than necessary.

As for your program design, if you know that you are going to input
twenty numbers, why don't you allocate space for twenty numbers to begin
with? Use another variable to store how many numbers have been input so far.



Actually, this is one of the problems in the textbook I'm using to
study C.
The requirement is that I need to use smallest possible array, which I
took to mean that I can't use an array of size 20 from the start.
As mentioned in one of the repies, it does seem inefficient to keep
creating and destroying arrays, and I'm trying to find another way.
Thanks for all the help!
 
S

santosh

Actually, this is one of the problems in the textbook I'm using to
study C.
The requirement is that I need to use smallest possible array, which I
took to mean that I can't use an array of size 20 from the start.
As mentioned in one of the repies, it does seem inefficient to keep
creating and destroying arrays, and I'm trying to find another way.

You could create a 20 element array using malloc in one go, at program
start-up, and then fill in unique values into the array, as you receive
them, keeping track of the number of used elements. Then after input
has finished, you can re-size the array to just the number of used
elements with a single call to realloc.

This way there are at most two allocation calls and in the case of 20
unique values, just one.

Be aware though that for numbers as small as twenty the "re-size" may
not actually deallocate the unneeded elements. It depends on your
implementation of malloc and realloc.
 
J

jxh

Actually, this is one of the problems in the textbook I'm using
to study C. The requirement is that I need to use smallest
possible array, which I took to mean that I can't use an array
of size 20 from the start.

If the point of the exercise is to use the least amount of memory
possible, you must either make multiple passes over your input to
determine how many duplicates you have, so you can allocate the
correct amount of memory to store the non duplicates, or perform
something close to what you originally wanted, and realloc the
memory up 1 by 1 to hold each non duplicate you encounter.

More efficient algorithms exist to get the answer in a single pass
in linear time, but requires knowing the bounds of the input, and
would use considerable more memory than the array that holds the
answer.

With C99, you could perhaps play some fancy tricks with VLAs and
recursion, but it might be more trouble than it is worth.

As others have pointed out, C does not have a garbage collector
per se, other than auto variables being deallocated when they fall
out of scope.

-- James
 
K

Keith Thompson

Christopher Benson-Manica said:
[comp.lang.c] Keith Thompson said:
Standard C has no *automatic* garbage collection.

I would not say that free() constitutes "manual" garbage collection
either, although that may be splitting a meaningless semantic hair.

The point being that it's not "garbage collection", yes?

(I predict that someone will now pounce on this and complain about
pedantry, totally ignoring the fact that you already acknowledged that
it "may be splitting a meaningless semantic hair".)
 
C

CBFalconer

I'm trying to write a program that saves numbers without any
duplications. I was thinking I could start by storing the first
number in an array with size one, and if the next number is not in
the array I've created, I save the first number and the new input
number in a new array with size two and delete the first array
from memory. This goes on until total of twenty numbers are input
into the program.

But is it possible to delete things from memory yourself?
Isn't there anything like garbage collector in C?

No, there is no garbage collection in C. But it is not even useful
in most cases.

You are headed for programs with amazingly long run-times.
Consider using a hashtable for this application. You can find all
the code for one in hashlib.zip, written in standard C and licensed
under GPL, and found at:

<http://cbfalconer.home.att.net/download/>
 
S

santosh

CBFalconer said:
No, there is no garbage collection in C. But it is not even useful
in most cases.

You are headed for programs with amazingly long run-times.
Consider using a hashtable for this application. You can find all
the code for one in hashlib.zip, written in standard C and licensed
under GPL, and found at:

<http://cbfalconer.home.att.net/download/>

Don't you think a hash table for just twenty input values is overkill?
 
U

user923005

Do you think that 20 values is the end objective?

Suppose that there are never more than 20 values, but they must be
accessed millions of times per second?

In such a case, I think a hash table will still be a pretty good
choice.
 
F

Franz Hose

I'm trying to write a program that saves numbers without any
duplications.
I was thinking I could start by storing the first number in an array
with size one,
and if the next number is not in the array I've created, I save the
first number and the new input number
in a new array with size two and delete the first array from memory.
This goes on until total of twenty numbers are input into the program.

But is it possible to delete things from memory yourself?
Isn't there anything like garbage collector in C?

i use lcc-win32 for that. has garbage collection. worx gr8!
 
C

CBFalconer

Franz said:
i use lcc-win32 for that. has garbage collection. worx gr8!

And your code runs only on Windoze, and compiles only on the
nearly-C lcc-win32. Fine for throw aways.

You need to examine your spell checker. 'i' needs to be
capitalized, and worx and gr8 have no connection to the English
language.
 
F

Friedrich Dominicus

And your code runs only on Windoze, and compiles only on the
nearly-C lcc-win32. Fine for throw aways.
May all be but the Boehm-Weisser GC surely run on other platform but
Windows. And even throw away can evolve ;-) I even doubt that the
Windows platform is espcially well supported...

Regards
Friedrich
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top