473,398 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Reference Counted Smart Pointers And STL Containers

Hi,

I was wondering, since STL containers are based around copying, whether
it's a good idea to use reference counted smart pointers, such as
boost::shared_ptr in STL containers.
I can't store the objects directly in a container, because they must
not be duplicated, so I have to use pointers. I'm just not certain about
using raw pointers or some kind of smart pointer.

Regards,
Matthias
Aug 27 '05 #1
5 2556

Matthias Kaeppler skrev:
Hi,

I was wondering, since STL containers are based around copying, whether
it's a good idea to use reference counted smart pointers, such as
boost::shared_ptr in STL containers.
I can't store the objects directly in a container, because they must
not be duplicated, so I have to use pointers. I'm just not certain about
using raw pointers or some kind of smart pointer.

Regards,
Matthias


A reference-counted pointer such as boost::shared_ptr is almost always
the correct choice in situations such as this.

/Peter

Aug 27 '05 #2
pe***************@gmail.com wrote:
A reference-counted pointer such as boost::shared_ptr is almost always
the correct choice in situations such as this.


Hm, yes probably you're right. It's just that I'm getting segfaults on
program exit, and it's almost certainly due to the smart pointers I'm
holding in a std::set. I'm not sure what I'm doing wrong.
Is there anything special I have to watch out for when storing a
boost::shared_ptr in an STL container?

Regards,
Matthias
Aug 27 '05 #3
Matthias Kaeppler wrote:
pe***************@gmail.com wrote:
A reference-counted pointer such as boost::shared_ptr is almost always
the correct choice in situations such as this.


Hm, yes probably you're right. It's just that I'm getting segfaults on
program exit, and it's almost certainly due to the smart pointers I'm
holding in a std::set. I'm not sure what I'm doing wrong.
Is there anything special I have to watch out for when storing a
boost::shared_ptr in an STL container?

Regards,
Matthias


Could you post a (small if possible) piece of code that demonstrates the
problem?
Best

Kai-Uwe Bux
Aug 27 '05 #4
Kai-Uwe Bux wrote:
Could you post a (small if possible) piece of code that demonstrates the
problem?


Sure. I have started a new thread dealing with the problem directly, I
will post the relevant parts of my code there.
Aug 27 '05 #5

Matthias Kaeppler wrote:
Hi,

I was wondering, since STL containers are based around copying, whether
it's a good idea to use reference counted smart pointers, such as
boost::shared_ptr in STL containers.
I can't store the objects directly in a container, because they must
not be duplicated, so I have to use pointers. I'm just not certain about
using raw pointers or some kind of smart pointer.

Regards,
Matthias


If you want to make sure you don't have duplicates, consider using
either a clone smart pointer or a COW smart pointer.
See following links:
http://code.axter.com/copy_ptr.h
http://code.axter.com/cow_ptr.h
http://code.axter.com/clone_ptr.h

Both the copy_ptr and clone_ptr class will make sure you only have
unique pointers, and have strict pointer ownership logic, which means
they do NOT share their pointers.

cow_ptr does share the pointer, until an attempt is made to access the
non-constant version of the pointer. At that point, it will clone
(copy) itself.

Sep 10 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: David B. Held | last post by:
I wanted to post this proposal on c.l.c++.m, but my news server apparently does not support that group any more. I propose a new class of exception safety known as the "smart guarantee". ...
4
by: Eric | last post by:
See question in main function below...TIA. struct A {}; struct B: public A {}; #include <boost/shared_ptr.hpp> #include <set> typedef boost::shared_ptr<A> AP; typedef std::set<AP> AS;
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
6
by: zl2k | last post by:
hi, When I considered about preventing memory leaking, the method came up to my mind is using boost smart pointer if possible (use stl::vector instead of type, use smart pointer whenever declare...
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
29
by: shuisheng | last post by:
Dear All, The problem of choosing pointer or reference is always confusing me. Would you please give me some suggestion on it. I appreciate your kind help. For example, I'd like to convert a...
1
by: oec.deepak | last post by:
Hi Cn any one telll me what is Reference counting in C++.
68
by: Jim Langston | last post by:
I remember there was a thread a while back that was talking about using the return value of a function as a reference where I had thought the reference would become invalidated because it was a...
1
by: PeterAPIIT | last post by:
What is template template arguments and template typename arguments ? The reason i write in policy based design is because this is the requirement of the assignment. My code so far: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.