reference counting

Discussion in 'C++' started by Tony Johansson, Aug 14, 2005.

  1. Hello Experts!

    I reading a book called programming with design pattern revealed
    by Tomasz Muldner and here I read something that I don't understand
    completely.

    It says
    "A garbarage collector, such as the one used in Java, maintains a record of
    whether or not
    an object is currentlys being used. An unused object is tagged as garbage,
    which means
    that it can be collected and returned to the pool of available memory. One
    simple
    technique used to implement a garbage collector is called reference
    counting: Multiple objects share a single representation that keeps track of
    the number of objects currently in use.
    Reference counting is useful in everyday programming; for example, you
    can use a string class, in which multiple objects can share the same
    representation."

    Now to my first question what does it mean with this sentence "Multiple
    objects share a single representation that keeps track of the number of
    objects currently in use."
    Does it mean that many object share another object called X and in this
    another object X is a reference counter that count the number of object
    reference this object X.

    Now to my second question what does it mean with this sentence "you
    can use a string class, in which multiple objects can share the same
    representation". This second question is the last part of sentence
    "Reference counting is useful in everyday programming; for example, you
    can use a string class, in which multiple objects can share the same
    representation."

    Many thanks

    //Tony
     
    Tony Johansson, Aug 14, 2005
    #1
    1. Advertising

  2. Tony Johansson

    Pete Becker Guest

    Tony Johansson wrote:

    [snipped] <g>

    There are several ways to implement reference counting. One way is to
    create a wrapper type that holds a pointer to the resource and a pointer
    to a memory block that holds the reference count. The copy operations on
    objects of this type increment the reference count, and the destructor
    decrements the reference count. (That's a bit simplified, but good
    enough for now) When the reference count goes to zero the memory block
    that holds the reference count gets freed and the resource gets freed.
    So as long as there's one or more of these wrapper objects around the
    resource still exists. When the last one goes away the resource goes
    away, too.

    That's not the same as garbage collection. A garbage collector is part
    of the memory manager. When the application runs out of memory the
    garbage collector paws through the application's data structures,
    following pointers to see which heap blocks still have live pointers to
    them. Blocks that don't have live pointers are garbage, and available
    for recycling.

    The difference between the two is twofold. First, with reference
    counting, the controlled resource is released as soon as the last
    reference to it goes away, with garbage collection the memory for the
    controlled resource is released when the garbage collector needs it,
    which might never happen. Second, with reference counting you have to
    worry about circular data structures. For example, suppose a node holds
    a reference-counted pointer to another node, and that second node holds
    a reference-counted pointer to the first node. Even if all of the
    program's references to these two nodes are gone, their reference counts
    are both still one, because each holds a pointer to the other. They'll
    never get recycled. With a garbage collector this is no problem. Neither
    one is reachable from the program's data, so they both get recycled.

    As to that mention of string classes, it's not necessarily true. Some
    implementations of the standard string class use reference counting, and
    some don't.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
     
    Pete Becker, Aug 14, 2005
    #2
    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. ash
    Replies:
    1
    Views:
    425
    SenderX
    Oct 24, 2003
  2. Kalle Rutanen

    Reference counting in C++

    Kalle Rutanen, May 7, 2005, in forum: C++
    Replies:
    0
    Views:
    567
    Kalle Rutanen
    May 7, 2005
  3. Tony Johansson

    reference counting

    Tony Johansson, May 22, 2005, in forum: C++
    Replies:
    4
    Views:
    375
    Andrew Koenig
    May 23, 2005
  4. mathieu
    Replies:
    8
    Views:
    518
    Juha Nieminen
    Aug 31, 2008
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    213
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page