Question

Based on this description of reference counting below:

"A normal "reference counting" idiom involves a 'shared object' (with the count), and simple 'reference objects' (not C++ references, although the semantics would be similar) which refer to the shared object. The constructors and destructors of the 'reference objects' are responsible for calling the incref/decref methods on the shared object. So the shared object is automatically counting the number of active 'reference objects."(reference counted class and multithreading)

i have search internet and found this example:

namespace Optimized {
struct StringBuf {
    StringBuf();             // start off empty
   ~StringBuf();             // delete the buffer
    void Reserve( size_t n );// ensure len >= n

    char*    buf;            // allocated buffer
    size_t   len;            // length of buffer
    size_t   used;           // # chars actually used
    unsigned refs;           // reference count
};

class String {
  public:
    String();                // start off empty
   ~String();                // decrement reference count
                             //  (delete buffer if refs==0)
    String( const String& ); // point at same buffer and
                             //  increment reference count
    void   Append( char );   // append one character
  private:
    StringBuf* data_;
 };
}

  namespace Optimized {

   StringBuf::StringBuf() : buf(0), len(0), used(0), refs(1) { }

   StringBuf::~StringBuf() { delete[] buf; }

   void StringBuf::Reserve( size_t n ) {
    if( len < n ) {
    size_t newlen = max( len * 1.5, n );
    char*  newbuf = new char[ newlen ];
    copy( buf, buf+used, newbuf );

    delete[] buf;   // now all the real work is
    buf = newbuf;   //  done, so take ownership
    len = newlen;
    }
  }

  String::String() : data_(new StringBuf) { }
  String::~String() {
  if( --data_->refs < 1 ) {
    delete data_;
      }
    }
   String::String( const String& other )
    : data_(other.data_)
    {
      ++data_->refs;
    }
 }

Is this example satisfies the conditions from description above? I mean, Does it involve shared object(is it StringBuf struct in that case?) and reference objects( String class)?

Was it helpful?

Solution

It is (almost*) never a good idea to mix business logic with life cycle management.

For this reason shared ownership with reference counting in C++11 standard library and well known Boost library is implemented in separate template classes encapsulating most commonly required ownership schemes.

In Boost library these are:

  • shared_ptr<T> - shared ownership with reference counting (reference counter is allocated when the first instance of shared_ptr<T> for particular raw pointer T* is created).

  • weak_ptr<T> - a handle that can be used to obtain full shared_ptr<T> if that one is still alive

  • *intrusive_ptr<T> - shared ownership with reference counting where reference counter is a part of the object being managed. This particular class is an example of what you are trying to achieve, but already polished to industrial standard.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top