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;
public:
    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);
    p->DoSomething();
    delete p;
}

You can write:

void foo()
{
    auto_ptr<MyClass> p(new MyClass);
    p->DoSomething();
}

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++.

Links:

http://ootips.org/yonat/4dev/smart-pointers.html

2412 questions
1954
votes
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
  • 91,635
  • 50
  • 223
  • 320
267
votes
17 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?
fen
  • 8,812
  • 5
  • 29
  • 53
241
votes
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. …
user383352
  • 4,443
  • 4
  • 22
  • 20
234
votes
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…
sbi
  • 204,536
  • 44
  • 236
  • 426
226
votes
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?
Sasha
198
votes
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
  • 9,537
  • 14
  • 52
  • 64
195
votes
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
  • 234,851
  • 74
  • 306
  • 532
169
votes
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…
michaelk
  • 1,947
  • 3
  • 14
  • 18
166
votes
6 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?…
sdgfsdh
  • 24,047
  • 15
  • 89
  • 182
160
votes
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…
laurent
  • 79,308
  • 64
  • 256
  • 389
149
votes
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));
NFRCR
  • 4,606
  • 4
  • 29
  • 34
123
votes
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…
AJG85
  • 14,891
  • 12
  • 40
  • 50
121
votes
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…
Venemo
  • 17,191
  • 9
  • 78
  • 117
115
votes
11 answers

Smart pointers: who owns the object?

C++ is all about memory ownership - aka ownership semantics. It is the responsibility of the owner of a chunk of dynamically allocated memory to release that memory. So the question really becomes who owns the memory. In C++ ownership is documented…
Martin York
  • 234,851
  • 74
  • 306
  • 532
108
votes
4 answers

Where is shared_ptr?

I am so frustrated right now after several hours trying to find where shared_ptr is located. None of the examples I see show complete code to include the headers for shared_ptr (and working). Simply stating std, tr1 and is not helping at…
Jake
  • 10,633
  • 20
  • 79
  • 134
1
2 3
99 100