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 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 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
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
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_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. 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". ...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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:
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |