Questions tagged [smart-pointers]

An abstract data type that simulates a pointer while providing additional features, such as automatic garbage collection or bounds checking

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. What could these things be? Probably the most common bugs in C++ (and C) are related to pointers and memory management: dangling pointers, memory leaks, allocation failures and other joys. Having a smart pointer take care of these things can save a lot of aspirin...

The simplest example of a smart pointer is auto_ptr, which is included in the standard C++ library (C++03). You can find it in the header <memory>, or take a look at Scott Meyers' auto_ptr implementation. Here is the relevant parts of auto_ptr's implementation, to illustrate what it does:

template <class T> class auto_ptr
    T* ptr;
    explicit auto_ptr(T* p = 0) : ptr(p) {}
    ~auto_ptr()                 {delete ptr;}
    T& operator*()              {return *ptr;}
    T* operator->()             {return ptr;}
    // ...

As shown, auto_ptr is a simple wrapper around a regular pointer. It forwards all meaningful operations to this pointer (dereferencing and indirection). Its "smartness" is in the destructor: the destructor takes care of deleting the pointer.

For the user of auto_ptr, this means that instead of writing:

void foo()
    MyClass* p(new MyClass);
    delete p;

You can write:

void foo()
    auto_ptr<MyClass> p(new MyClass);

And trust p to clean-up after itself.

Smart pointers form part of the idiomatic RAII (Resource Acquisition Is Initialisation) technique that is core to resource management in C++.


2763 questions
14 answers

What is a smart pointer and when should I use one?

What is a smart pointer and when should I use one?
Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
18 answers

Is there any use for unique_ptr with array?

std::unique_ptr has support for arrays, for instance: std::unique_ptr p(new int[10]); but is it needed? probably it is more convenient to use std::vector or std::array. Do you find any use for that construct?
  • 9,835
  • 5
  • 34
  • 57
2 answers

Why can I not push_back a unique_ptr into a vector?

What is wrong with this program? #include #include int main() { std::vector> vec; int x(1); std::unique_ptr ptr2x(&x); vec.push_back(ptr2x); //This tiny command has a vicious error. …
  • 5,033
  • 4
  • 25
  • 20
4 answers

Which kind of pointer do I use when?

Ok, so the last time I wrote C++ for a living, std::auto_ptr was all the std lib had available, and boost::shared_ptr was all the rage. I never really looked into the other smart pointer types boost provided. I understand that C++11 now provides…
  • 219,715
  • 46
  • 258
  • 445
4 answers

smart pointers (boost) explained

What is the difference between the following set of pointers? When do you use each pointer in production code, if at all? Examples would be appreciated! scoped_ptr shared_ptr weak_ptr intrusive_ptr Do you use boost in production code?
4 answers

std::auto_ptr to std::unique_ptr

With the new standard coming (and parts already available in some compilers), the new type std::unique_ptr is supposed to be a replacement for std::auto_ptr. Does their usage exactly overlap (so I can do a global find/replace on my code (not that…
Martin York
  • 257,169
  • 86
  • 333
  • 562
6 answers

RAII and smart pointers in C++

In practice with C++, what is RAII, what are smart pointers, how are these implemented in a program and what are the benefits of using RAII with smart pointers?
Rob Kam
  • 10,063
  • 14
  • 55
  • 65
8 answers

Why would I std::move an std::shared_ptr?

I have been looking through the Clang source code and I found this snippet: void CompilerInstance::setInvocation( std::shared_ptr Value) { Invocation = std::move(Value); } Why would I want to std::move an std::shared_ptr?…
  • 33,689
  • 26
  • 132
  • 245
4 answers

Differences between std::make_unique and std::unique_ptr with new

Does std::make_unique have any efficiency benefits like std::make_shared? Compared to manually constructing std::unique_ptr: std::make_unique(1); // vs std::unique_ptr(new int(1));
  • 5,304
  • 6
  • 32
  • 37
2 answers

Using smart pointers for class members

I'm having trouble understanding the usage of smart pointers as class members in C++11. I have read a lot about smart pointers and I think I do understand how unique_ptr and shared_ptr/weak_ptr work in general. What I don't understand is the real…
  • 2,107
  • 3
  • 15
  • 18
6 answers

How much is the overhead of smart pointers compared to normal pointers in C++?

How much is the overhead of smart pointers compared to normal pointers in C++11? In other words, is my code going to be slower if I use smart pointers, and if so, how much slower? Specifically, I'm asking about the C++11 std::shared_ptr and…
  • 18,515
  • 13
  • 84
  • 125
8 answers

Why do C++ libraries and frameworks never use smart pointers?

I read in a few articles that raw pointers should almost never be used. Instead they should always be wrapped inside smart pointers, whether it's scoped or shared pointers. However, I noticed that frameworks like Qt, wxWidgets and libraries like…
  • 88,262
  • 77
  • 290
  • 428
4 answers

Passing shared_ptr as shared_ptr

What is the best method to go about passing a shared_ptr of a derived type to a function that takes a shared_ptr of a base type? I generally pass shared_ptrs by reference to avoid a needless copy: int foo(const shared_ptr& ptr); but this…
Matt Kline
  • 10,149
  • 7
  • 50
  • 87
3 answers

What C++ Smart Pointer Implementations are available?

Comparisons, Pros, Cons, and When to Use? This is a spin-off from a garbage collection thread where what I thought was a simple answer generated a lot of comments about some specific smart pointer implementations so it seemed worth starting a new…
  • 15,849
  • 13
  • 42
  • 50
2 answers

How to return smart pointers (shared_ptr), by reference or by value?

Let's say I have a class with a method that returns a shared_ptr. What are the possible benefits and drawbacks of returning it by reference or by value? Two possible clues: Early object destruction. If I return the shared_ptr by (const) reference,…
Vincenzo Pii
  • 18,961
  • 8
  • 39
  • 49
2 3
99 100