There is a lot of pointers in C++ but to be honest in 5 years or so in C++ programming (specifically with the Qt Framework) I only use the old raw pointer:
SomeKindOfObject *someKindOfObject = new SomeKindOfObject();
I know there are a lot of other "smart" pointers:
// shared pointer:
shared_ptr<SomeKindofObject> Object;
// unique pointer:
unique_ptr<SomeKindofObject> Object;
// weak pointer:
weak_ptr<SomeKindofObject> Object;
But I don't have the slightest idea of what to do with them and what they can offer me in comparison of raw pointers.
For example I have this class header:
#ifndef LIBRARY
#define LIBRARY
class LIBRARY
{
public:
// Permanent list that will be updated from time to time where
// each items can be modified everywhere in the code:
QList<ItemThatWillBeUsedEveryWhere*> listOfUselessThings;
private:
// Temporary reader that will read something to put in the list
// and be quickly deleted:
QSettings *_reader;
// A dialog that will show something (just for the sake of example):
QDialog *_dialog;
};
#endif
This is clearly not exhaustive but for each of these 3 pointers is it OK to leave them "raw" or should I use something more appropriate?
And in the second time, if an employer will read the code, will he be strict on what kind of pointers I use or not?
Best Answer
A "raw" pointer is unmanaged. That is, the following line:
... will leak memory if an accompanying
delete
is not executed at the proper time.auto_ptr
In order to minimize these cases,
std::auto_ptr<>
was introduced. Due to the limitations of C++ prior to the 2011 standard, however, it's still very easy forauto_ptr
to leak memory. It is sufficient for limited cases, such as this, however:One of its weakest use-cases is in containers. This is because if a copy of an
auto_ptr<>
is made and the old copy is not carefully reset, then the container may delete the pointer and lose data.unique_ptr
As a replacement, C++11 introduced
std::unique_ptr<>
:Such a
unique_ptr<>
will be correctly cleaned up, even when it's passed between functions. It does this by semantically representing "ownership" of the pointer - the "owner" cleans it up. This makes it ideal for use in containers:Unlike
auto_ptr<>
,unique_ptr<>
is well-behaved here, and when thevector
resizes, none of the objects will be accidentally deleted while thevector
copies its backing store.shared_ptr
andweak_ptr
unique_ptr<>
is useful, to be sure, but there are cases where you want two parts of your code base to be able to refer to the same object and copy the pointer around, while still being guaranteed proper cleanup. For example, a tree might look like this, when usingstd::shared_ptr<>
:In this case, we can even hold on to multiple copies of a root node, and the tree will be properly cleaned up when all copies of the root node are destroyed.
This works because each
shared_ptr<>
holds on to not only the pointer to the object, but also a reference count of all of theshared_ptr<>
objects that refer to the same pointer. When a new one is created, the count goes up. When one is destroyed, the count goes down. When the count reaches zero, the pointer isdelete
d.So this introduces a problem: Double-linked structures end up with circular references. Say we want to add a
parent
pointer to our treeNode
:Now, if we remove a
Node
, there's a cyclic reference to it. It'll never bedelete
d because its reference count will never be zero.To solve this problem, you use a
std::weak_ptr<>
:Now, things will work correctly, and removing a node will not leave stuck references to the parent node. It makes walking the tree a little more complicated, however:
This way, you can lock a reference to the node, and you have a reasonable guarantee it won't disappear while you're working on it, since you're holding on to a
shared_ptr<>
of it.make_shared
andmake_unique
Now, there are some minor problems with
shared_ptr<>
andunique_ptr<>
that should be addressed. The following two lines have a problem:If
thrower()
throws an exception, both lines will leak memory. And more than that,shared_ptr<>
holds the reference count far away from the object it points to and this can mean a second allocation). That's not usually desirable.C++11 provides
std::make_shared<>()
and C++14 providesstd::make_unique<>()
to solve this problem:Now, in both cases, even if
thrower()
throws an exception, there will not be a leak of memory. As a bonus,make_shared<>()
has the opportunity to create its reference count in the same memory space as its managed object, which can both be faster and can save a few bytes of memory, while giving you an exception safety guarantee!Notes about Qt
It should be noted, however, that Qt, which must support pre-C++11 compilers, has its own garbage-collection model: Many
QObject
s have a mechanism where they will be destroyed properly without the need for the user todelete
them.I do not know how
QObject
s will behave when managed by C++11 managed pointers, so I can not say thatshared_ptr<QDialog>
is a good idea. I do not have enough experience with Qt to say for sure, but I believe that Qt5 has been adjusted for this use case.