10

What exactly is reference counting? In particular, what is it for C++? What are the problems we can face if we don't handle them? Do all languages require reference counting?

Thomas Eding
  • 35,312
  • 13
  • 75
  • 106
Naruto
  • 9,476
  • 37
  • 118
  • 201
  • 2
    Start reading [here](http://www.gotw.ca/gotw/043.htm). – Xeo Apr 06 '12 at 19:20
  • 1
    "Reference counting is a common optimization (also called "lazy copy" and "copy on write")." - Herb Sutter. WTF? Reference counting is definitely *not* the same as "lazy copy" or "copy on write". Reference counting (is typically) for memory management. It is not an optimization per-se. – Thomas Eding Apr 06 '12 at 19:28
  • The [wikipedia page on reference counting](http://en.wikipedia.org/wiki/Reference_counting) is a pretty good introduction to the subject. – André Caron Apr 06 '12 at 19:29
  • @trinithis: Indeed. Copy-on-write is a special use case for reference counting. I don't have the books at hand (not at home at the moment). Any idea if it's been corrected in the book version? – André Caron Apr 06 '12 at 19:29

4 Answers4

8

What exactly is reference counting? In particular, what is it for C++?

In simple words, Reference counting means counting the references to an object.

Typically, C++ employs the technique of RAII. Wherein, the ability to manage the deallocation of an type object is tied up within the type object itself. It means that the user does not have to explicitly manage the lifetime of the object and its deallocation explicitly, The functionality to do this management is built in the object itself.

This functionality means that the object should exist and remain valid untill there are stakeholders who refer to the object, and this is achieved by reference counting. Everytime the object is shared(copied) the reference count(typically a member inside the class type) is incremented and each time the destructor is called the count is decremented, when the count reaches 0, the object is not being reffered by anyone and it marks the end of its lifetime and hence it is destructed.

What are the problems we can face if we don't handle them?

It would mean no more RAII, and endless and often faulty manual resource management.
In short programming nightmares.

Do all languages require reference counting?

Languages don't require reference counting but employing the technique provides very easy usage and less efforts for users of the language, So most languages prefer to use it to provide these advantages to their users.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
1

Reference counting is a simple but not complete approach for garbage detection.

When the counter reaches zero, you could release that object.

BUT if there are no more used objects which referencing each other cyclic, they will never be released

Consider a references b, b references a, but nothing else reference a or b. The reference count on a and b will be still 1 (= in use)

stefan bachert
  • 9,413
  • 4
  • 33
  • 40
  • 2
    Strictly speaking, you're not "garbage collecting" when you have reference-counted resources, since there is no garbage. Garbage is what you get *after* all references have been lost. C++ generally doesn't produce garbage. (Unlike C++ *programmers*, perhaps.) – Kerrek SB Apr 06 '12 at 19:35
  • Right, that is why I used the term garbage DETECTION – stefan bachert Apr 06 '12 at 19:37
  • Why *Garbage*? There is no garbage its merely *resource lifetime management*, fancy name ***RAII***. – Alok Save Apr 06 '12 at 19:42
  • In general the term "garbage" is used for objects which are no more accessible for others. Your usage of "garbage" is rather memory leak, no more accessible for anyone (but the os) but still allocated. – stefan bachert Apr 06 '12 at 19:50
  • if "garbage" would not possible in c++, this would be nonsense http://www.hpl.hp.com/personal/Hans_Boehm/gc/ – stefan bachert Apr 06 '12 at 19:53
0

Reference-count garbage collection is a powerful technique for managing memory that helps prevent objects from being deleted accidentally or more than once. The technique is not limited to C++ code and, despite its name, is unrelated to the C++ concept of reference variables. Rather, the term means that we maintain a count of all ``owning references'' to an object and delete the object when this count becomes zero.

IndieProgrammer
  • 579
  • 1
  • 5
  • 14
-2

Reference counting - lets use a metaphor.

You have an ear. You want it back at some point.

You get a group of people pointing at your ear. You count them as soon as they point.

When the number goes to zero - it is just yours and you can do with it as you wish.

I.e. take it out of the equation (free it back to memory).

BTW. Circular stuff is tricky to spot.

Ed Heal
  • 59,252
  • 17
  • 87
  • 127
  • 3
    "You have an ear. You want it back at some point." Well, I already have it, no need to want it back. It's also my ear when a dozen people point at it. Flawed metaphor. –  Feb 01 '13 at 09:57