Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A question about reference counting

Reply
Thread Tools

A question about reference counting

 
 
Tony Johansson
Guest
Posts: n/a
 
      08-14-2005
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



 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      08-14-2005
Tony Johansson wrote:

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


That's a bit sloppily worded.

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


Yes. You have one object that contains the actual data and some number of
references to it. The object maintains a reference count that contains the
number of references that refer to that object. When the reference count
reaches 0, there is no reference to the object anymore, so it can't be used
anymore (since the references are the only way of accessing the object),
which means it can be destroyed.

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


It means the same thing. You have something like:

MyString a = "Hello";
MyString b = a;

Now there are two MyString objects, but those are only references to the
actual string data, which is shared between both objects. Instead of
copying the string data, b just refers to the same data as a, and the
reference count is incremented when b is created.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reference counting and API (const reference vs pointer oriented) mathieu C++ 8 08-31-2008 09:05 AM
counting up instead of counting down edwardfredriks Javascript 6 09-07-2005 03:30 PM
reference counting Tony Johansson C++ 4 05-23-2005 01:28 PM
Reference counting in C++ Kalle Rutanen C++ 0 05-07-2005 12:26 PM
flyweight reference counting ash C++ 1 10-24-2003 10:40 AM



Advertisments