Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using smart pointers in containers

50 views
Skip to first unread message

Paul

unread,
Jul 17, 2015, 9:00:52 AM7/17/15
to
Some have said on other threads to use raw pointers instead of unique_ptr when iterating in a container. However, other advice has been just the opposite. For example http://eli.thegreenplace.net/2012/06/20/c11-using-unique_ptr-with-standard-library-containers and http://marknelson.us/2012/06/24/c11-unique_ptrt/

Suppose, for example, that I'm designing a tree where the number of nodes descending from any particular node is variable. Any reason not to put nodes in vectors of std::unique_ptr<T>

Thank you,

Paul

bartekltg

unread,
Jul 17, 2015, 11:10:18 AM7/17/15
to
On 17.07.2015 15:00, Paul wrote:
> Some have said on other threads to use raw pointers instead of
> unique_ptr when iterating in a container.

" iterating in a container."
I don't understand.

It looks like all 'gurus' from c++ say to use unique pointers.

It is all about keeping RAII in dynamic alocated objects. About
who own what. If you container of pointers is a main owner,
of objects, no other long living object 'borrow it',
unique_ptr is a good idea.
There:
https://www.youtube.com/user/CppCon
somewhere is quite good lecture about poiters and optimalizations,
and speaker said if you have function that not relocate ownership,
raw pointers are best solution. Chain of function have raw pointer
as argument, and the top function is call with unique_ptr
as an argument.

There is something about this topic
https://www.youtube.com/watch?v=xnqTKD8uD64

https://github.com/CppCon/CppCon2014/blob/master/Presentations/Back%20to%20the%20Basics!%20Essentials%20of%20Modern%20C%2B%2B%20Style/Back%20to%20the%20Basics!%20Essentials%20of%20Modern%20C%2B%2B%20Style%20-%20Herb%20Sutter%20-%20CppCon%202014.pdf

And lecture is very interesting, but I'm sure there was one more focused
on pointers. Sorry, I don't remember which one.


> Suppose, for example, that I'm designing a tree where the number of
> nodes descending from any particular node is variable. Any reason
> not to put nodes in vectors of std::unique_ptr<T>

If you don't keep pointers (raw in case of uniqueptr) in other places
I think it is nice idea.

In a node you erase one node (pointing at children) from vector. This
is unique_ptr, so it runs destructor of targeted node. And node have a
vector of unique ptr, so destructor of vestor and all pointers is
called...
You erased one node, all children (recursively) are erased and its
memory freed automatically.
One function less to write ;-)


But it works only because it is a tree.

bartekltg

Öö Tiib

unread,
Jul 17, 2015, 11:12:06 AM7/17/15
to
On Friday, 17 July 2015 16:00:52 UTC+3, Paul wrote:
> Some have said on other threads to use raw pointers instead of unique_ptr
> when iterating in a container.

No. Write special wrapper classes around raw pointers that
are called iterator for iterating/navigating in containers.

You can alternatively use the raw pointers directly as poor
man's iterators but that exposes all the details of navigation.

There are no way how to use smart pointers for navigating
because all what smart pointers are doing is managing
resources and that is exactly something that someone who
is navigating around in those managed resources does not
want to do.

> However, other advice has been just the opposite.
> For example http://eli.thegreenplace.net/2012/06/20/c11-using-unique_ptr-with-standard-library-containers

Read it again. It is suggesting to use 'unique_ptr' as the
data that is *stored* in standard containers nowhere as
iterators that are *navigating* in containers.

and http://marknelson.us/2012/06/24/c11-unique_ptrt/

Same there.

> Suppose, for example, that I'm designing a tree where the
> number of nodes descending from any particular node is variable.
> Any reason not to put nodes in vectors of std::unique_ptr<T>

You can put C++11 smart pointers into all standard containers
('std::auto_ptr' was suitable for only some). You can use the
smart pointers for building your own containers (but you have
to avoid circular references). However you can't use smart pointer
as iterator of your container.

Richard

unread,
Jul 20, 2015, 6:52:50 PM7/20/15
to
[Please do not mail me a copy of your followup]

Paul <peps...@gmail.com> spake the secret code
<792871fe-dc6e-46f5...@googlegroups.com> thusly:
Use shared_ptr<> and unique_ptr<> to indicate ownership lifetimes.

There's no reason to stop using local variables (including parameters)
that are raw pointers when it is clear that this does not imply a
transfer of ownership.

Usually, but not always, a container of items allocated on the heap
manages the lifetime of those items. If those items are shared, then
it's a container of shared_ptrs, if those items are uniquely owned by
the container, then it's a container of unique_ptr.

There are times when a container does not manage the lifetime of the
pointers inside it (temporary data structures built up during
processing are a good example) and in these cases, it could be
disastrous to have a container of smart pointers because the container
doesn't own the resources pointed to by the raw pointers.
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>
0 new messages