what's a smart pointer

Started by
6 comments, last by Erik Labree 23 years, 4 months ago
Hello there, I some docs a read they talk about smart pointers. What''s the difference between a ''normal'' pointer and a ''smart'' pointer. Thanks. Erik
Advertisement
A common problem in large projects featuring dynamic memory allocation is 'reference counting,' i.e., keeping track of the number of references still active in various parts of the program. For obvious reasons, deallocating memory that is still referenced somewhere is a recipe for disaster.
You may have noticed in Microsoft Windows that COM objects are reference counted. All COM interfaces are derived from the basic interface IUnknown, which provides the methods AddRef (used to indicate to the underlying COM object that you are cloning the reference, e.g., pCOM_2=pCOM_1; pCOM_2->AddRef(); ) and Release (used to indicate the destruction of a reference, e.g., pCOM_2->Release(); pCOM_2=NULL; )
Unfortunately, this leave a lot of housekeeping up to the programmer. One solution is to replace all variables of type IUnknown* with variables of a type that specifically supports reference counting. For example,

template < class T > struct Reference
{
T* ptr;

void ChangePointer(T* _ptr)
{
if (ptr) ptr->Release();
ptr=_ptr;
if (ptr) ptr->AddRef();
}

T* operator->(void) const { return ptr; }
// Assumes T is derived from IUnknown
Reference& operator=(T* _ptr)
{ ChangePointer(_ptr); return *this; }
Reference& operator=(const Reference& _ref)
{ ChangePointer(_ref.ptr); return *this; }

// etc.

Reference() : ptr(NULL) { }
Reference(T* _ptr) : ptr(_ptr) { }
~Reference() { ChangePointer(NULL); }
};

I've typed this all in off the top of my head, but I'm sure you get the idea. I'll leave it up to you to work out the rest of the details.

yours,
Mox.


Edited by - mox on November 28, 2000 5:48:54 AM
A smart pointer is a wrapper around a normal pointer so that the pointer will be a stack based rather than heap based and therefore the normal pointer it wraps will be deleted (because the destructor calls delete) when the smart pointer goes out of scope (whether by exception or normal exit)

The STL autoptr is an example of a smart pointer

  Function(){   C *p = new C();              // a normal pointer   std::autoptr<C> sp(new C()); // a smart pointer}  
Good point, Void. I suppose, more generally, a smart pointer is any type that supports all normal pointer conventions and provides some additional utility (in my example, reference counting).

yours,
Mox.
In Visual C++ 1.0, it was also a compiler option for base/derived class pointers. You could specifiy (if I remember correctly) single, multiple, smart, and some other. The smart option used the most efficient representation possible. Here''s a snippet from the docs for a similar option found in Borland C++ 4.52:

"When the Smallest for Class option is on, member pointers use the smallest possible representation that allows member pointers to point to all members of their particular class. If the class is not fully defined at the point where the member pointer type is declared, the most general representation is chosen by the compiler (a warning is issued)."



David
I think that had to do with the difference between, near, far, & huge pointers; not sucidal pointers (if(refcount=0) delete this.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
No that had nothing to do with near, far, etc. It has to do with compiler symbol table representation and methods to access member pointers of classes. (A big deal when dealing with multiple inheritance.) It''s still with us today, in MSVC, look up the compiler options /vmb and /vmg. They just don''t call it smart pointers anymore. Probably due to the conflict in terminology with classes that overide operator->.
Thanks for the discussion guys.

It''s become clear now.

Regards Erik

This topic is closed to new replies.

Advertisement