Isn't there anything like garbage collector in C?

Discussion in 'C Programming' started by xicloid@gmail.com, Oct 8, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. 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 <at> doc.ic.ac.uk
     
    Philip Potter, Oct 8, 2007
    #2
    1. Advertising

  3. writes:
    > 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.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 8, 2007
    #3
  4. [comp.lang.c] Keith Thompson <> wrote:

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

    --
    C. Benson Manica | I appreciate all corrections, polite or otherwise.
    cbmanica(at)gmail.com |
    ----------------------| I do not currently read any posts posted through
    sdf.lonestar.org | Google groups, due to rampant unchecked spam.
     
    Christopher Benson-Manica, Oct 8, 2007
    #4
  5. Richard Guest

    Christopher Benson-Manica <> writes:

    > [comp.lang.c] Keith Thompson <> wrote:
    >
    >> 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.
     
    Richard, Oct 8, 2007
    #5
  6. santosh Guest

    Richard wrote:

    > Christopher Benson-Manica <> writes:
    >
    >> [comp.lang.c] Keith Thompson <> wrote:
    >>
    >>> 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.
     
    santosh, Oct 8, 2007
    #6
  7. Guest

    On 10 8 , 6 12 , Philip Potter <> wrote:
    > 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 <at> doc.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, 2007
    #7
  8. santosh Guest

    wrote:

    > On 10 8 , 6 12 , Philip Potter <> wrote:
    >> 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.
     
    santosh, Oct 8, 2007
    #8
  9. jxh Guest

    On Oct 8, 10:11 am, wrote:
    > On 10 8 , 6 12 , Philip Potter <> wrote:
    >
    > > 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
     
    jxh, Oct 8, 2007
    #9
  10. Christopher Benson-Manica <> writes:
    > [comp.lang.c] Keith Thompson <> wrote:
    >> 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".)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Oct 8, 2007
    #10
  11. CBFalconer Guest

    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
     
    CBFalconer, Oct 8, 2007
    #11
  12. santosh Guest

    CBFalconer wrote:

    > 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?
     
    santosh, Oct 8, 2007
    #12
  13. CBFalconer Guest

    santosh wrote:
    > CBFalconer wrote:
    >> 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
     
    CBFalconer, Oct 9, 2007
    #13
  14. user923005 Guest

    On Oct 8, 5:23 pm, CBFalconer <> wrote:
    > santosh wrote:
    > > CBFalconer wrote:
    > >> 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.
     
    user923005, Oct 9, 2007
    #14
  15. Franz Hose Guest

    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!
     
    Franz Hose, Oct 10, 2007
    #15
  16. CBFalconer Guest

    Franz Hose wrote:
    > 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
     
    CBFalconer, Oct 11, 2007
    #16
  17. CBFalconer <> writes:


    >
    > 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.
     
    Friedrich Dominicus, Oct 11, 2007
    #17
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Tillie

    Garbage Collector Debugging

    Rob Tillie, Aug 15, 2003, in forum: ASP .Net
    Replies:
    11
    Views:
    1,765
    JerryK
    Aug 18, 2003
  2. Pyramis
    Replies:
    0
    Views:
    403
    Pyramis
    Jan 25, 2004
  3. Colt

    Garbage collector problem

    Colt, Nov 15, 2003, in forum: Java
    Replies:
    9
    Views:
    689
    Tim Ward
    Nov 18, 2003
  4. bart59
    Replies:
    0
    Views:
    503
    bart59
    Jun 17, 2004
  5. Mark Sizzler
    Replies:
    12
    Views:
    660
    Tom Anderson
    Nov 21, 2008
Loading...

Share This Page