What exactly does counting in C ++ refer to ?,

What is link counting? In particular, what is it for C ++? What are the problems that we may face if we do not handle them? Does all languages ​​require reference counting?

+8
c ++ constructor copy-constructor
source share
4 answers

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

In simple words, reference counting means counting references to an object.

C ++ typically uses the RAII technique. In this case, the ability to control the release of an object of type is tied to the type of object itself. This means that the user does not need to explicitly control the lifetime of the object and its release explicitly. The functionality for this control is built into the object itself.

This function means that the object must exist and remain valid as long as there are interested parties that reference the object, and this is achieved by counting the links. Each time an object is divided (copied), the reference counter (usually a member within the class type) increases and each time the destructor is called a counter, decreases when the counter reaches 0, the object is not referenced by anyone, and it marks the end of it life and therefore collapses.

What are the problems we may encounter if we do not handle them?

This would mean no more than RAII, but endless and often erroneous resource management manually.
In short, programming nightmares.

Does all languages ​​require reference counting?

Languages ​​do not require reference counting, but using this method provides very simple use and less effort for language users. Therefore, most languages ​​prefer to use it to provide these benefits to their users.

+7
source share

Link counting is a simple but incomplete approach to garbage detection.

When the counter reaches zero, you can free this object.

BUT, if there are no more usable objects that reference each other cyclically, they will never be released

Consider links b, b of link a, but nothing more than link a or b. reference to a and b will remain 1 (= when using)

+1
source share

Link-garbage collection is a powerful way to manage memory that helps prevent accidental or accidental deletion of objects. This method is not limited to C ++ code and, despite its name, is not related to the concept of C ++ reference variables. Rather, this term means that we support the counting of all "power links" to the object and delete the object when this count becomes zero.

0
source share

Link Count - allows you to use a metaphor.

You have an ear. At some point you want to return it.

You get a group of people pointing to the ear. You count them as soon as they indicate.

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

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

BTW. Circular material is difficult to detect.

-2
source share

All Articles