CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Feb 2002
    Posts
    5,757

    Scott Meyers' Effective STL :: C++

    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

  2. #2
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    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

  3. #3
    Join Date
    Feb 2002
    Posts
    5,757
    Okay. Thanks.

    Kuphryn

  4. #4
    Join Date
    Aug 2000
    Location
    New Jersey
    Posts
    968
    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.
    David Maisonave
    Author of Policy Based Synchronized Smart Pointer
    http://axter.com/smartptr


    Top ten member of C++ Expert Exchange.
    C++ Topic Area

  5. #5
    Join Date
    Feb 2002
    Posts
    5,757
    Okay. Thanks.

    I understand the concept now.

    Kuphryn

  6. #6
    Join Date
    Feb 2002
    Posts
    5,757
    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

  7. #7
    Join Date
    Mar 2002
    Location
    California
    Posts
    1,582
    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

  8. #8
    Join Date
    Feb 2002
    Posts
    5,757
    Okay.

    Kuphryn

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured