Click to See Complete Forum and Search --> : Scott Meyers' Effective STL :: C++
kuphryn
October 12th, 2002, 10:35 PM
Hi.
I am studying more advanced C++ via Scott Meyer's Effective STL. I have not read any of his previous books including the C++ series. He does an exceptional job introducing invaluable advices on the use of STL. I am still reading it.
He makes references to a term that I am not familiar with. He mentions "reference counting" about vector and string containers. What is it?
Thanks,
Kuphryn
jfaust
October 12th, 2002, 11:21 PM
Sometimes it's not obvious in a design who has ownership of a resource, be it memory for a class instance, or a file handle, or anything else. It's usually used in terms of dynamic memory for a class instance.
Since ownership is difficult to determine, it's similarly difficult to determine object lifetime. In other words, we don't know when to delete the object. The quick answer is "when it's done being used." That's not easy to determine in all cases. Well that's when reference counting comes in.
Whenever any thing holds a pointer for any length of time, that thing, class or function, must say it's holding a reference to it. Usually it does it by calling a method, usually called addRef or similar, on the object. When it's done using the object, it calls another method, usually called release.
The object, in turn, increments an internal counter on every addRef and decrements it on every release. When the counter reaches zero, it deletes itself.
This is a very powerful means of maintaining memory, and becomes even more powerful when used in conjunction with smart pointers. Smart pointers can remove the responsibility of calling addRef and release from the client, which is an easy thing to forget.
boost's shared_ptr template uses a very sophisticated form of this technique.
Jeff
kuphryn
October 12th, 2002, 11:33 PM
Okay. Thanks.
Kuphryn
Axter
October 13th, 2002, 08:12 AM
Most std::string implementations and CString have reference counting.
The reference counting method used in std::string and CString can increase the speed of the code, especially when values are passed by value instead of my referance.
Example:
std::string str_a = "Hello World";
std::string str_b = str_a;
With referance counting, str_b doesn't have to initiate a buffer and copy the data from str_a to str_b. Instead it increases the referance counter, and has it's buffer pointing to str_a's buffer.
So you have the equalvilant of the following code:
str_a.ref_count++;
str_b.buf_pointer = str_a.buf_pointer;
If std::string implementation was NOT using reference counting, then the code required would be equalvilant to the following code:
str_b.buf_pointer = new char[str_a.Len];
strcpy(str_b.buf_pointer, str_a.buf_pointer);
Creating a buffer and copying the data to it, takes up much more time then increasing a number and changing a pointer.
kuphryn
October 13th, 2002, 10:26 AM
Okay. Thanks.
I understand the concept now.
Kuphryn
kuphryn
October 13th, 2002, 10:32 AM
What do you think about reference counting in terms of optimization and encapsulation? I understand its fundamental, but I do not understand the concept behind reference counting. In other words, how does it enchance C++ language?
Kuphryn
jfaust
October 13th, 2002, 10:45 AM
It allows an object to determine it's own lifetime, removing the responsibility from the client, who may not be able to easily determine when to delete it.
Reference counted strings give a performance boost, in both memory consumption and speed. The encapsulation aspect of this is that the client of the string class doesn't care wether it's reference counted or not. It's an implementation detail.
It's not part of the C++ language, so it doesn't really enhance it.
Jeff
kuphryn
October 13th, 2002, 10:58 AM
Okay.
Kuphryn
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.