Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Erik Labree

what's a smart pointer

This topic is 6588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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->.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!