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 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 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
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
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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;
|
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
|
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
|
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?
| |
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);
|
by: oec.deepak |
last post by:
Hi
Cn any one telll me what is Reference counting in C++.
|
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 );
}
|
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"
|
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,...
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |