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

2763 questions
1
vote
2 answers

std::unique_ptr with std::map

I have a std::map where the key is std::shared_ptr and the value is std::unique_ptr where Foo and Bar are very different classes from a third-party library. I am using this std::map object as an in-memory cache. I am wondering what the…
cmpbedes
  • 473
  • 1
  • 5
  • 13
1
vote
1 answer

Creating a smart pointer member of class referencing itself is a elegant design pattern in c++?

I would like to know if following code is a good pattern in C++? There is no problem at all. The code works. But I would like to know if this can lead to some sort of problem. #include #include template class Class…
arthur.afarias
  • 140
  • 1
  • 9
1
vote
1 answer

Link error when using std::make_unique when creating object with const char *

Apologies for not providing simple runnable failure code. The error is part of the larger codebase that would require a lot of refactoring. I'm running into a very weird linking problem with my code that so far I can't solve. I have a class with…
ilya1725
  • 4,496
  • 7
  • 43
  • 68
1
vote
1 answer

Using Smart Pointers With FFTW3

In the fftw3 documentation the standard example is: #include ... { fftw_complex *in, *out; fftw_plan p; ... in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N); out = (fftw_complex*)…
Daniel Marchand
  • 584
  • 8
  • 26
1
vote
0 answers

Atomic swap for smart pointers on x86/x86-64

Is it possible to implement atomic swap for smart pointers on x86/x86-64 without using of transactional memory. Especially interested in atomic exchange for shared_ptr. There are CMPXCHG8B and CMPXCHG16B instructions on modern processors. Can they…
Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
1
vote
5 answers

Please explain this expression

class TestPtr : protected QSharedPointer where Test is an abstract interface class. The TestPtr class should serve as the smart pointer class. Does this mean class TestPtr is derived from the class Test ? Is class test enclosed in a smart…
Vij
  • 27
  • 1
  • 7
1
vote
1 answer

Ownership with a physical representation

After reading on RAII, viewing Herb Sutter's CppCon2014 presentation, and reading the core guidelines and related articles over the course of some days, I'm still quite confused on ownership and related semantics. Let's say class A and class B…
aPonza
  • 492
  • 4
  • 10
1
vote
1 answer

"Template typedef" inside a copy constructor doesn't work

Some background: I'm writing a policy-based smart pointer (like SmartPtr in the Loki library), which can have destructive copy semantics like auto_ptr. Therefore, it needs to have a template copy constructor taking non-const reference to modify the…
lizarisk
  • 7,562
  • 10
  • 46
  • 70
1
vote
2 answers

Sharing objects owned via smart pointer

Basically i have a one struct that contains objects to share between classes as following; struct CoreComponents { std::unique_ptr m_A = std::make_unique(); std::unique_ptr m_B; std::unique_ptr m_C =…
Nuri
  • 65
  • 1
  • 9
1
vote
1 answer

std::vector of Base class with Derived objects

I have a deque of std::shared_ptr declared has a deque of a base class (let's call it Polygon) but I'm storing on it pointers to derived objecs (for example Triangle). I'm thinking now about change the data structure to be a std::vector to use the…
1
vote
2 answers

Overloading operator= of user defined type with unique_ptr data member

I have a user defined class that has a std::unique_ptr member. I am trying to overload the assignment operator to new an object of the same type to the unique_ptr member or assign the value to the ptr value. class object { public: // POD…
Nathan Krone
  • 63
  • 1
  • 9
1
vote
2 answers

Calling `delete` on an object owned by a `unique_ptr` using another pointer

I have a pointer to class initialized by the new operator. Then I use this pointer to setup a std::unique_ptr. Now, as far as my understanding goes, the following code has double delete, once the manually called delete operator and then when the…
mahesh
  • 1,028
  • 10
  • 24
1
vote
3 answers

How to design smart pointer of array type in C++11

This is a class which contains image data. class MyMat { public: int width, height, format; uint8_t *data; } I want to design MyMat with automatic memory management. The image data could be shared among many objects. Common APIs which I'm…
shang12
  • 423
  • 5
  • 18
1
vote
2 answers

deep copy with smart pointer-attached inherited objects

I am not sure what the best / cleanest solution to making a deep copy of an object with a smart pointer containing inherited objects is. To boil it down, given the following code class A {}; class D1 : public A{ public: int x1 = 0; }; class…
ulf
  • 190
  • 1
  • 6
1
vote
1 answer

C++: How do I pass this pointer into this class?

Here's a simple piece of code. What I want help with is the two places I've writen "???". The goal is to pass a temporary pointer to the container-class which then becomes the unique owner of that object. However, I do not know what to write in the…
Ieao
  • 23
  • 2
1 2 3
99
100