?
Solved

Smart Pointer

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

Thanks in advance
Mahesh
0
Comment
Question by:MaheshKP
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 60 total points
ID: 2587353
Opps that was supposed to be an asnwer.
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2587785
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
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.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

800 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