Boost logo

Boost :

From: Borgerding, Mark A. (MarkAB_at_[hidden])
Date: 2000-01-28 11:48:53

Hash: SHA1

Hello boosters,
I am new on the mailing list but have followed boost for some time
now. I may have something to contribute.

I think I discovered a new approach for smart pointers (tell me if
this has been done). Instead of maintaining the reference count as
an integer created on the heap, the smart ptr objects internally
maintain a double-linked list in which they are members.

If a smart ptr object is the only node in the list, it is the last
reference and is .: responsible for cleaning up.

This approach has two distinct advantages over conventional ref.
counting techniques:
1) linked_ptrs need not allocate heap resources. This means no
allocators would be needed for generic use.
2) Fast! I compared linked_ptr with boost::shared_ptr using the
following code, and linked_ptr was more than 4 times faster!
   //typedef boost::shared_ptr<foo> smartptr;
   typedef linked_ptr<foo> smartptr;
   foo * dumbptr = new foo(42);// don't want to time foo's
   boost::timer t;
   smartptr sp(dumbptr);//c'tor from dumb *
   dumbptr = NULL;
   for (int i=0;i<1000;++i) {
      smartptr s2(sp); //copy c'tor
      smartptr s3; // default c'tor
      s3 = sp; // assign
   cout << "Took " << t.elapsed() << "s using " <<
typeid(smartptr).name() << endl;

Attached is linked_ptr.hpp. The class has basically the same
interface as shared_ptr, except the function use_count() has been
omitted, since it would require linear complexity. Personally, I've
never needed to know anything about a reference count except whether
or not it was unique.

Also attached is a version of timer.cpp that I changed to allow more
precise measurement (I #ifdef'd some non-portable code into it). If
you try to run the above test code without these extensions, you'll
need to bump up the i<1000 number considerably to get any non-zero

I am interested in your comments & suggestions.

Incidentally, my dev. platforms are MSVC6 (NT4.0 on P2) at work and
gcc 2.95.2 (Mandrake 6.0 on Pentium)
Anybody know where to get a good, free <limits> header for gcc? The
Cygnus snapshots seem to lack this. I cannot fully test my timer.cpp
changes without it.

Mark Borgerding
markab (at) xetron . com
Software Engineer
Xetron Corporation

Version: PGPfreeware 6.5.2 for non-commercial use <>


Boost list run by bdawes at, gregod at, cpdaniel at, john at