garbage collector

Discussion in 'C++' started by Ganesh, Sep 22, 2005.

  1. Ganesh

    Ganesh Guest

    Hi,

    How does a garbage collector (in C++), like mark and sweep, work on
    heap
    allocated arrays? In this case,

    1. The only live variable on stack that points to the memory may be the
    one that holds the starting address of the array. Or,

    2. There could be a stack variable that points to an address that is,
    say, in the middle of the array.

    How does GC mark the entire array as live in these cases?

    Thanks,
    Ganesh
    Ganesh, Sep 22, 2005
    #1
    1. Advertising

  2. Ganesh

    Howard Guest

    "Ganesh" <> wrote in message
    news:...
    >
    > Hi,
    >
    > How does a garbage collector (in C++), like mark and sweep, work on
    > heap
    > allocated arrays? In this case,
    >
    > 1. The only live variable on stack that points to the memory may be the
    > one that holds the starting address of the array. Or,
    >
    > 2. There could be a stack variable that points to an address that is,
    > say, in the middle of the array.
    >
    > How does GC mark the entire array as live in these cases?
    >


    I wasn't aware there *was* any garbage collection in standard C++. If it's
    something provided by one of your compilers, then you should probably ask in
    a newsgroup or help files for that compiler.

    -Howard
    Howard, Sep 22, 2005
    #2
    1. Advertising

  3. Ganesh

    Guest

    Howard wrote:
    > "Ganesh" <> wrote in message
    > news:...


    > > How does a garbage collector (in C++), like mark and sweep, work on
    > > heap
    > > allocated arrays?

    [...]
    > I wasn't aware there *was* any garbage collection in standard C++. If it's
    > something provided by one of your compilers, then you should probably ask in
    > a newsgroup or help files for that compiler.


    Good advice, but GC is not necessarily a compiler feature in C or C++.
    It can be implemented as a library:

    http://www.hpl.hp.com/personal/Hans_Boehm/gc/

    That page seems to contain the answers the OP is looking for.

    Ali
    , Sep 23, 2005
    #3
  4. Ganesh

    Ganesh Guest

    I posted my question in this group because:

    1. Searching for "Garbage Collection" in groups home, yielded
    comp.lang.c++.moderated as the first link. Search for "Garabage
    Collector" yeilded 3rd link to the same group. And there were atleast 3
    links to this group in the first 10 results. Though, I posted it in
    the 'moderated' version, it was not accepted. It is ridiculous, as
    there is a disucussion in the same group on a very similar topic and
    153 messages have been posted.

    2. I used only C++ as my primary programming language. My question had
    some assumptions, like having a pointer, which is true in C++ and not
    JAVA, and hence I posted it here.

    Ganesh
    Ganesh, Sep 23, 2005
    #4
  5. Ganesh

    msalters Guest

    Ganesh schreef:

    > I posted my question in this group because:
    >
    > 1. Searching for "Garbage Collection" in groups home, yielded
    > comp.lang.c++.moderated as the first link. Search for "Garabage
    > Collector" yeilded 3rd link to the same group. And there were atleast 3
    > links to this group in the first 10 results. Though, I posted it in
    > the 'moderated' version, it was not accepted. It is ridiculous, as
    > there is a disucussion in the same group on a very similar topic and
    > 153 messages have been posted.


    GC isn't always offtopic, as it is possible to combine C++ and GC.
    However,
    blue-sky discussions about every possible combination wouldn't make
    sense.

    > 2. I used only C++ as my primary programming language. My question had
    > some assumptions, like having a pointer, which is true in C++ and not
    > JAVA, and hence I posted it here.


    Actually, Java does have pointers, and they're more suited to GC use
    than
    C++ pointers (no reinterpret_cast<> between int and Object in Java)
    There are a lot more languages which use pointers, even if they expose
    them only to their internal GC mechanism. That too makes your question
    a GC question, an not a C++ question.

    Compare: a question about numerical computing is off-topic here, even
    if it happened to be implemented in C++. valarray<> would be ontopic.

    HTH,
    Michiel Salters
    msalters, Sep 23, 2005
    #5
  6. "Ganesh" <> wrote in message
    news:...
    >
    > Hi,
    >
    > How does a garbage collector (in C++), like mark and sweep, work on
    > heap
    > allocated arrays?


    There is no difference between array of objects and single object.
    They are all allocated as memory blocks.

    In this case,
    >
    > 1. The only live variable on stack that points to the memory may be the
    > one that holds the starting address of the array. Or,
    >
    > 2. There could be a stack variable that points to an address that is,
    > say, in the middle of the array.
    >
    > How does GC mark the entire array as live in these cases?


    GC does not know about arrays. It just tracks pointers to memory area that
    is
    bound by GC allocated heap.
    (I think that you asked about hans boehm gc implementation)
    hope this helps.

    Greetings, Bane.
    Branimir Maksimovic, Sep 23, 2005
    #6
    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,756
    JerryK
    Aug 18, 2003
  2. Pyramis
    Replies:
    0
    Views:
    395
    Pyramis
    Jan 25, 2004
  3. Colt

    Garbage collector problem

    Colt, Nov 15, 2003, in forum: Java
    Replies:
    9
    Views:
    681
    Tim Ward
    Nov 18, 2003
  4. bart59
    Replies:
    0
    Views:
    497
    bart59
    Jun 17, 2004
  5. Will Hartung

    Garbage Collector Tuning?

    Will Hartung, Sep 8, 2004, in forum: Java
    Replies:
    3
    Views:
    2,092
    Kevin McMurtrie
    Sep 11, 2004
Loading...

Share This Page