473,796 Members | 2,690 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_p tr 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 2575

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_p tr 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_p tr 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_p tr 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_p tr 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_p tr 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_p tr 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_p tr 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
2687
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". Essentially, the smart guarantee promises to clean up resources whose ownership is passed into the function, for whatever defintion of "clean up" is most appropriate for the resource passed. Note that this is different from both the basic and the...
4
1363
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
9969
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 must be an object instead of
6
2311
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 an instance of class). With the container of STL, this may often result in a vector of smart pointers, or a smart pointer of STL container. Am I making things too complicated or this is an usual implementation? Thanks in advance. zl2k
92
5131
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, but I just don't know. I don't like them because I don't trust them. I use new and delete on pure pointers instead. Do you use smart pointers?
29
3656
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 string to a integer number. bool Convert(const string& str, int* pData); bool Convert(const string& str, int& data);
1
2278
by: oec.deepak | last post by:
Hi Cn any one telll me what is Reference counting in C++.
68
4660
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 temporary but it was stated that it would not. This has come up in an irc channel but I can not find the original thread, nor can I get any code to work. Foo& Bar( int Val ) { return Foo( Val ); }
1
1494
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: #include "StoragePolicy.h"
0
9527
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10172
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7546
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.