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. These objects are flexible as pointers and have the advantage of being an object (like constructor and destructors called automatically). 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: 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.

Smart Pointers

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 in C++ > Smart Pointers 101

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. C++ Smart Pointers. Smart Pointers FAQ (and code to use!)

C++ Smart Pointers

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.

Yonat's Stuff for Developers. Smart Pointers - What, Why, Which? Yonat Sharon What are they?

Smart Pointers - What, Why, Which?

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.