Could you please give me some advice on GC in C?

Discussion in 'C Programming' started by byang, Apr 22, 2009.

  1. byang

    byang Guest

    Hi,
    I am now designing a library in C, and the libary dynamically
    allocate much memory, and now I use reference couting to deal with
    memory alloc/free. I mean, the client of this libary should call unref
    () for many pointer of the data structure of the library. And this ref/
    unref interface impose more additional task for client programmers.
    But I am wondering is there a better way? So, I goole for "gc for C",
    and got a mark-sweep collector (http://www.hpl.hp.com/personal/
    Hans_Boehm/gc/). Could anybody here please give me some advice on GC
    of the library? Thanks a lot!


    Regards!
    Bo
     
    byang, Apr 22, 2009
    #1
    1. Advertisements

  2. byang

    Guest Guest

    GC isn't part of the standard. But the Boehm collector is the best
    known one. lcc-win which is implemented by a regular on clc has a GC.
    It may also use the Boehm collector. I dunno. Ask on an lcc-win
    specific
    ng or look for their website for more details.
     
    Guest, Apr 22, 2009
    #2
    1. Advertisements

  3. byang

    jacob navia Guest

    The lcc-win C compiler proposes the GC in the standard distribution.
    It is running in the 32 bit version and has been ported in the 64 bit
    version.

    Under the linux OS there is gc library easily avilable. The software
    from Boehm runs also in a wide variety of environments.

    Actually it is VERY EASY to use. Just replace all malloc calls by
    GC_malloc, and drop all calls to free().

    You can download the lcc-win compiler at the URL below
     
    jacob navia, Apr 22, 2009
    #3
  4. byang

    CBFalconer Guest

    Take a look at nmalloc, which is built for use with DJGPP. It
    includes a full set of debuggery, which will show you faults,
    unfreed areas, etc. If you can adapt it to your system go ahead.
    See:

    <http://cbfalconer.home.att.net/download/nmalloc.zip>
     
    CBFalconer, Apr 23, 2009
    #4
  5. byang

    Gene Guest

    Reference counting is probably the simplest way to gc library-
    allocated objects. However, it has the worst performance among gc
    algorithms. You could consider implementing your own gc within the
    library. It's pretty easy to write a simple mark-and-sweep or even a
    2-space copying collector on top of malloc and free. The ugly part is
    that your library caller must register/unregister gc roots. This may
    be no worse than reference counting, however. To see one way of doing
    this, look at the code for emacs.

    Boehm's collector is a beautiful idea, but, as others have said, it
    has significant practical problems. Code like

    char *s = malloc(1000);
    scanf("%s", s);
    int len = 0;
    while (*s++) {
    malloc(1 << 27); // induce a gc
    len++;
    }
    printf("%d\n", len);

    will fail with Boehm because the induced gc will not find any pointer
    to the start of the 1000 char string, and the string will be
    collected. Other more subtle problems occur in less contrived code
    due to aggressive optimizations by compilers.
     
    Gene, Apr 23, 2009
    #5
  6. byang

    jacob navia Guest

    This is completely wrong.

    I compiled following program:
    #include <gc.h>
    int main(void)
    {
    char *s = GC_malloc(1000);
    scanf("%s", s);
    int len = 0;
    while (*s++) {
    GC_malloc(1 << 27); // induce a gc
    len++;
    }
    printf("%d\n", len);
    }

    And the following run happened:
    d:\lcc\mc76\test>tgc
    abc
    3

    Any pointer to the inside part of a block will avoid it being collected
    by the gc.



    Other more subtle problems occur in less contrived code

    Yeah sure.

    But maybe you can give an example?
     
    jacob navia, Apr 23, 2009
    #6
  7. byang

    jacob navia Guest

    This is utterly vague. Which papers please?
    What paper?
    That will be collected next gc if the integer is no longer in the stack
    The gc software has been running with lcc-win since 4-5 years at least.
    Your vague allegations doesn't cut it. Any specific example?
    Yes, this is described in the documentation.

    Why should use the example of the pointer being written to a file
    then retrieved,an example that the regulars here use since ages.

    GC doesn't fix the flaws of your program. True. And doesn't make you the
    coffee. You STILL have to walk to the coffee machine (shudder).
     
    jacob navia, Apr 23, 2009
    #7
  8. byang

    Ian Collins Guest

    Speaking as one who has used a GC with C (to keep a leaky binary only
    application running and to check for memory leaks in better ones), I
    agree with Jacob. I've never seen any problems.
     
    Ian Collins, Apr 23, 2009
    #8
  9. byang

    CBFalconer Guest

    #include <stdlib.h>

    void longtime(void) {
    /* this takes 1/2 hour to execute */
    return;
    }

    int main(void) {
    char *ptr;

    if (!(ptr = malloc(128))) exit(EXIT_FAILURE);
    ptr += 128;
    longtime();
    ptr -= 128;
    *ptr = 'A';
    free(ptr);
    return 0;
    }

    So, after coding longtime() to agree with the comment, do you claim
    this program is flawed? Admitted, I had to work at it.
     
    CBFalconer, Apr 24, 2009
    #9
  10. What is this code meant to show? You might want to pick an example
    where all the ptr stuff can't be removed by the optimiser (or was that
    part of the point?).

    I have a feeling it shows you don't know how GC would work in C.
     
    Ben Bacarisse, Apr 24, 2009
    #10
  11. byang

    CBFalconer Guest

    Possibly true. I am assuming the GC system scans memory for a
    pointer to the memory area. If it doesn't find one, it frees that
    memory block. ptr+128 points just past the block, so is
    legitimate. It could well point to the next block assigned. It is
    not dereferenced, so that does not create an error. So I expect
    that during the execution of longtime() the allocated block will be
    freed. The access by *ptr (which is valid) will fail. The free
    (which is legitimate) will fail.

    Am I wrong in this analysis? If so, where.
     
    CBFalconer, Apr 24, 2009
    #11
  12. byang

    Flash Gordon Guest

    I would expect a conservative GC to NOT free a block with a pointer to
    one past it for the simple reason that it CAN be a valid pointer for
    that block. It may indeed cause it t not free another block. That is why
    conservative GCs are called "conservative"!
     
    Flash Gordon, Apr 24, 2009
    #12
  13. byang

    James Kuyper Guest

    I agree that it's an imbecilic argument that he's referring to; but it's
    your argument, not his. All RH is trying to do is help you understand
    why that is the case. That makes him look foolish only insofar as that
    seems to be a doomed cause.
     
    James Kuyper, Apr 24, 2009
    #13
  14. I think it is wrong to criticise the technique by assuming an
    incorrect implementation. I can't image a real GC getting this
    wrong. It must consider the one-past-the-end pointer to be a valid
    pointer and must not free the 128 byte block. Do you have any evidence
    that any GC makes such a basic error?

    The Boehm GC does not even touch malloc'd memory, BTW.
     
    Ben Bacarisse, Apr 24, 2009
    #14
  15. byang

    Chris Dollin Guest

    Are you sure? Because, what else is there to GC?
     
    Chris Dollin, Apr 24, 2009
    #15
  16. You allocate GC memory using GC_malloc (or, better, the macro
    GC_MALLOC) and, of course, one can arrange that this be a replacement
    for malloc but I understood the two heaps were usually separate.

    From http://www.hpl.hp.com/personal/Hans_Boehm/gc/simple_example.html

    Interaction with the system malloc

    It is usually best not to mix garbage-collected allocation with the
    system malloc-free. If you do, you need to be careful not to store
    pointers to the garbage-collected heap in memory allocated with the
    system malloc.
     
    Ben Bacarisse, Apr 24, 2009
    #16
  17. byang

    CBFalconer Guest

    No, he deliberately snipped the portion that shows his imbecility,
    below:
    ^^^^^

    Which pointed out that the code would require modification.
     
    CBFalconer, Apr 24, 2009
    #17
  18. byang

    CBFalconer Guest

    Where am I assuming even an incorrect implementation? All I am
    assuming is a method of deciding whether a pointer to anywhere
    within an allocated block exists. I could probably replace the
    +128 with writing a hex version of the pointer value, and restoring
    it later, which avoids all arguments about how clost the possible
    pointer must be.
     
    CBFalconer, Apr 24, 2009
    #18
  19. byang

    CBFalconer Guest

    You can't do that, because malloc has to be system sensitive. It
    has to know things not directly available, such as where to get
    memory, alignment requirements, etc.
     
    CBFalconer, Apr 24, 2009
    #19
  20. byang

    jameskuyper Guest

    Comments like that were never effective at getting you to recognize
    the existence of non-standard things; why should someone parodying
    your attitudes in order to demonstrate how ridiculous they are be any
    more reasonable about such things than you are?
     
    jameskuyper, Apr 24, 2009
    #20
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.