473,811 Members | 3,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1436
xi*****@gmail.c om 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.i nvalidwrote:
xicl...@gmail.c om 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.c om wrote:
On 10 8 , 6 12 , Philip Potter <p...@see.sig.i nvalidwrote:
>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.c om wrote:
On 10 8 , 6 12 , Philip Potter <p...@see.sig.i nvalidwrote:
xicl...@gmail.c om 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.c om 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.c om 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.c om wrote:
>>>
I'm trying to write a program that saves numbers without any
duplication s. 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...@yah oo.comwrote:
santosh wrote:
CBFalconer wrote:
xicl...@gmail.c om 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.c om 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

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!
4
2045
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 the set can connect to objects in the set. Every object inherits a interface to allow for a mark and sweep garbage collector (GC) and my implementation works correctly and efficiently but only in a single thread. How can I garbage collect in a...
4
13872
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 something. So I ask you, how do you force the garbage collector to run and clean up?
5
5498
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 going to fill up myObject with info....tons of them myObject = nothing System.GC.Collect()
13
3821
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 can manage resources as well as objects. The programming style is clear and easy, conforming to RAII (Resource Acquisition Is Initialization) idiom of C++ programmers. The memory usage is very efficient, acyclic garbage is
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...
142
6879
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 chunks and recycles them. This memory allocation strategy has been adapted to C (and C++) by the library written by Hans J Boehm and Alan J Demers. Why a Garbage Collector? -----------------------
8
1799
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 down. I must point out that a heck of a lot of data are being read in and manipulated, and I was wondering if there were any metrics to show much of a performance hit is occurring during the initialisation? If it turns out that the GC is having a...
206
8396
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 footprint which says: "Indeed, one often hears arguments against building exibility into an engineered sys- tem. For example, in the philosophy of the computer language Python it is claimed: \There should be one|and preferably only one|obvious...
0
9726
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
10647
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...
0
10384
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...
0
10130
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...
0
9204
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...
0
5553
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4338
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
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
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.