Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Smart Pointer

Posted on 2000-03-05
7
Medium Priority
?
295 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
Independent Software Vendors: 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

610 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