Link to home
Start Free TrialLog in
Avatar of MaheshKP
MaheshKP

asked on

Smart Pointer

Please explain me about the smart pointer

Thanks in advance
Mahesh
Avatar of nietod
nietod

there is no "the smart pointer"  Smart pointers  are classes that act like pointers.  different smart pointer classes may have different properties.  but the general ides behind them is that they are used to prevent memory leaks.  (i.e they insure that memory that is allocated is eventually deleted.) Basically when the pointer object is destroyed it deletes the memory it points to--or somethign to that effect depending on the nature of the smart pointer.  The STL has a smart pointer template class called auto_ptr<>.
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nietod seems to be having problems with "asnwers". It must come from having 300,000+ points!

A smart pointer is a pointer based on a C++ class, or better said a template class. And they can be used for a variety of purposes but importantly in COM.
   The rules of COM state that when an interface pointer is copied, the AddRef method must be called to increment the reference count. And when a pointer is no longer used, the Release method must be called to decrement the count. The reference count in a COM object is used to determine its life. Ie: when the count is zero the object may be destroyed. Breakage of these rules leads to memory leaks.

   If the pointer is implemented behind the scenes as a class, then on construction (ie: copy) AddRef is called, and on destruction the Release is called. Importantly the compiler run-time does this automatically particularly during exception handling. Pointer which go out of scope due to raising and handling an exception will be properly destroyed thus calling the Release method.
   Lastly one can use smart pointers in a really smart way. If you assign an interface pointer of a different type, the assignment operator can automatically call the QueryInterface method to get the required interface pointer. This simplifies calls to QueryInterface but makes the code almost unreadable, which is why most COM programmers don't like it.
You can find out all about these pointers in any book about Active Template Library or, for a simple usage, in a book on COM, for example Dale Rogerson's Inside COM from Microsoft Press.

(Nietod: just joking!)
Just to clarify a few points.

>> A smart pointer is a pointer based on a
>> C++ class, or better said a template class.
i.e. it is a class that acts like a pointer and it is often a template class, but not always.  (I use smart pointers extensively, but never as a template.)

The rest of that is about how smart pointers are often used in relation to COM.  That is just one of many uses of smart pointers.  Most have nothing to do with COM, although most uses will share similar benefits.
Avatar of MaheshKP

ASKER

ThanQ nietod