Solved

Smart Pointer

Posted on 2000-03-05
7
286 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now