Solved

Smart Pointer

Posted on 2000-03-05
7
288 Views
Last Modified: 2013-12-14
Please explain me about the smart pointer

Thanks in advance
Mahesh
0
Comment
Question by:MaheshKP
7 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 2587350
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<>.
0
 
LVL 22

Accepted Solution

by:
nietod earned 20 total points
ID: 2587353
Opps that was supposed to be an asnwer.
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2587785
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 27

Expert Comment

by:BigRat
ID: 2588948
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.
0
 
LVL 27

Expert Comment

by:BigRat
ID: 2588955
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!)
0
 
LVL 22

Expert Comment

by:nietod
ID: 2589494
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.
0
 

Author Comment

by:MaheshKP
ID: 2590181
ThanQ nietod
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to become programmer? 12 37
Losing latter half of command line in Visual Studio C++ online program 10 80
Copying WordPress Pages 5 82
designing in object programming 12 75
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question