473,412 Members | 2,075 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Isn't there anything like garbage collector in C?

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?

Oct 8 '07 #1
11 1388
xi*****@gmail.com wrote:
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.

--
Philip Potter pgp <atdoc.ic.ac.uk
Oct 8 '07 #2
On 10 8 , 6 12 , Philip Potter <p...@see.sig.invalidwrote:
xicl...@gmail.com wrote:
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.

--
Philip Potter pgp <atdoc.ic.ac.uk


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!

Oct 8 '07 #3
xi*****@gmail.com wrote:
On 10 8 , 6 12 , Philip Potter <p...@see.sig.invalidwrote:
>xicl...@gmail.com wrote:
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.
<snip>
>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.
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.
Oct 8 '07 #4
jxh
On Oct 8, 10:11 am, xicl...@gmail.com wrote:
On 10 8 , 6 12 , Philip Potter <p...@see.sig.invalidwrote:
xicl...@gmail.com wrote:
I'm trying to write a program that saves numbers without
any duplications.
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.
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

Oct 8 '07 #5
xi*****@gmail.com wrote:
>
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/>

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 8 '07 #6
CBFalconer wrote:
xi*****@gmail.com wrote:
>>
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/>
Don't you think a hash table for just twenty input values is overkill?

Oct 8 '07 #7
santosh wrote:
CBFalconer wrote:
>xi*****@gmail.com wrote:
>>>
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/>

Don't you think a hash table for just twenty input values is overkill?
Do you think that 20 values is the end objective?

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 9 '07 #8
On Oct 8, 5:23 pm, CBFalconer <cbfalco...@yahoo.comwrote:
santosh wrote:
CBFalconer wrote:
xicl...@gmail.com wrote:
>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/>
Don't you think a hash table for just twenty input values is overkill?

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.

Oct 9 '07 #9
xi*****@gmail.com wrote:
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!

Oct 9 '07 #10
Franz Hose wrote:
xi*****@gmail.com wrote:
>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!
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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>

--
Posted via a free Usenet account from http://www.teranews.com

Oct 11 '07 #11
CBFalconer <cb********@yahoo.comwrites:

>
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


--
Please remove just-for-news- to reply via e-mail.
Oct 11 '07 #12

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

Similar topics

1
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...
4
by: Pedro Miguel Carvalho | last post by:
Greetings. I'm creating a project that as a intricate relation between object kind of like a set where each object in the set can be connect to a subset of object of the set and objects not in...
4
by: IcedCrow | last post by:
I know I read everywhere that you "can't" force the garbage collector to run, and that you really have no control when it runs. However as a programmer I know that there is always a way around...
5
by: Ben | last post by:
Could someone please verify if what I am doing as follow is corrected: 1. when dealing with custom class objects: ..... public myObject as myClass myObject as New myClass .......here I am...
13
by: Mingnan G. | last post by:
Hello everyone. I have written a garbage collector for standard C++ application. It has following main features. 1) Deterministic Finalization Providing deterministic finalization, the system...
28
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...
142
by: jacob navia | last post by:
Abstract -------- Garbage collection is a method of managing memory by using a "collector" library. Periodically, or triggered by an allocation request, the collector looks for unused memory...
8
by: Paul.Lee.1971 | last post by:
Hi everyone, A program that I'm helping to code seems to slow down drastically during initialisation, and looking at the profiling graph, it seems to be the garbage collector thats slowing things...
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...
0
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...
0
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...

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.