background preloader

Smart Pointer

Facebook Twitter

Implementing a simple smart pointer in c++ Introduction What are smart pointers? The answer is fairly simple; a smart pointer is a pointer which is smart. What does that mean? Actually, smart pointers are objects which behave like pointers but do more than a pointer. Problems with pointers What are the common problems we face in C++ programs while using pointers? Char* pName = new char[1024]; … SetName(pName); … … if(null ! How many times have we found a bug which was caused because we forgot to delete pName. We shall start with a realistic example. Now we shall write the client code to use Person. void main() { Person* pPerson = new Person("Scott", 25); pPerson->Display(); delete pPerson; } Now look at this code, every time I create a pointer, I need to take care of deleting it. Void main() { SP p(new Person("Scott", 25)); p->Display(); } Note the following things: We have created an object of class SP which holds our Person class pointer.

Interface for a smart pointer Dereferencing (operator *) Indirection (operator ->) Applications. Smart Pointers. Introduction Common Requirements Exception Safety Exception-specifications History and Acknowledgements References Introduction Smart pointers are objects which store pointers to dynamically allocated (heap) objects. They behave much like built-in C++ pointers except that they automatically delete the object pointed to at the appropriate time. Smart pointers are particularly useful in the face of exceptions as they ensure proper destruction of dynamically allocated objects. They can also be used to keep track of dynamically allocated objects shared by multiple owners. Conceptually, smart pointers are seen as owning the object pointed to, and thus responsible for deletion of the object when it is no longer needed. The smart pointer library provides six smart pointer class templates: These templates are designed to complement the std::auto_ptr template.

Additionally, the smart pointer library provides efficient factory functions for creating shared_ptr objects: Common Requirements Rationale. Smart Pointers in C++ > Smart Pointers 101. Andrei Alexandrescu discusses smart pointers, from their simplest aspects to their most complex ones and from the most obvious errors in implementing them to the subtlest ones—some of which also happen to be the most gruesome. Smart pointers have been the subject of hecatombs of code written and rivers of ink consumed by programmers and writers around the world. Perhaps the most popular, intricate, and powerful C++ idiom, smart pointers are interesting in that they combine many syntactic and semantic issues. This chapter discusses smart pointers, from their simplest aspects to their most complex ones and from the most obvious errors in implementing them to the subtlest ones—some of which also happen to be the most gruesome.

In brief, smart pointers are C++ objects that simulate simple pointers by implementing operator-> and the unary operator*. This chapter not only discusses smart pointers but also implements a SmartPtr class template. So what's a smart pointer? C++ Smart Pointers. Smart Pointers FAQ (and code to use!) What is a "smart pointer"?

Smart pointer (as opposed to a "dumb" or "raw" pointer) is a C++ structure that behaves almost identically to a common C pointer but it also includes some other capabilities, e.g. throws an exception when it's NULL and someone tries to dereference it, or it destroys its contents automatically when it goes out of scope. What are the basic implementation schemes for smart pointers? In order to increase a pointer's IQ, you need to maintain somewhere some extra data about the object that it points to. There are only 3 places that this data may be stored: a) in the object itself b) in the pointer c) in some central depository In the first case, the smart pointers would need some kind of support from the objects themselves, therefore the scheme is usually referred to as intrusive scheme.

The other two cases are the non-intrusive schemes. What are the pros and cons of intrusive smart pointers? Enough with the theory. Yonat's Stuff for Developers. Smart Pointers - What, Why, Which? Yonat Sharon What are they? Smart pointers are objects that look and feel like pointers, but are smarter. What does this mean? To look and feel like pointers, smart pointers need to have the same interface that pointers do: they need to support pointer operations like dereferencing (operator *) and indirection (operator ->). An object that looks and feels like something else is called a proxy object, or just proxy. The proxy pattern and its many uses are described in the books Design Patterns and Pattern Oriented Software Architecture. To be smarter than regular pointers, smart pointers need to do things that regular pointers don't. The simplest example of a smart pointer is auto_ptr, which is included in the standard C++ library. As you can see, auto_ptr is a simple wrapper around a regular pointer.

For the user of auto_ptr, this means that instead of writing: void foo() { MyClass* p(new MyClass); p->DoSomething(); delete p; } You can write: And trust p to cleanup after itself. Why: Less bugs.