smart-pointers

pros and cons of smart pointers

I came to know that smart pointer is used for resource management and supports RAII. But what are the corner cases in which smart pointer doesn't seem smart and things to be kept in mind while using it ? ...

C++ design question (need cheap smart pointer)

I have a huge tree where keys insides nodes are indices into a big hash_map v, where v[key] is a (big) record associated with that key (includes how many nodes in the tree have this key). Right now, key is an integer. So each node has overhead of storing pointers for children and an integer. We can remove a key from a node in the tree. W...

boost signals - How control lifetime of objects sent to subscribers? Smart pointers?

I am using boost::signals2 under Red Hat Enterprise Linux 5.3. My signal creates an object copy and sends it's pointer to subscribers. This was implemented for thread safety to prevent the worker thread from updating a string property on the object at the same time it is being read ( perhaps I should revisit the use of locks? ). Anywa...

auto_ptr baffling behaviour

#include<iostream> #include<memory> #include<stdio> using namespace std; class YourClass { int y; public: YourClass(int x) { y= x; } }; class MyClass { auto_ptr<YourClass> p; public: MyClass() //:p(new YourClass(10)) { p= (auto_ptr<YourClass>)new YourClass(10); } MyClass( const MyClass &) : p(new Yo...

book about smart pointers

Hi All, Could you let me know about books that explains the ideas of smart pointer very clearly (beginner, intermediate and advanced level)? Thank you. ...

Advantages/disadvantages of auto pointers

What are the advantages and disadvantages of using auto pointers (auto_ptr), compared to ordinary pointers? I've heard it does automatic releasing of memory but how come it is not used often? ...

Questions on usages of shared_ptr - C++

I have few questions on the best practices of using shared_ptr. Question 1 Is copying shared_ptr cheap? Or do I need to pass it as reference to my own helper functions and return as value? Something like, void init_fields(boost::shared_ptr<foo>& /*p_foo*/); void init_other_fields(boost::shared_ptr<foo>& /*p_foo*/); boost::shared_ptr<...

Boost weak_ptr's in a multi-threaded program to implement a resource pool

I'm thinking of using boost::weak_ptr to implement a pool of objects such that they will get reaped when nobody is using one of the objects. My concern, though, is that it's a multi-threaded environment, and it seems there's a race condition between the last shared_ptr to an object going out of scope and a new shared_ptr being construct...

Compare shared_ptr with object created on stack

I have a situation where I would like to compare an object encapsulated by a shared_ptr with the same type of object created on a stack. Currently, I'm getting the raw pointer and dereferencing it to do the comparison eg: Object A; std::shared_ptr<Object> B; // assume class Object has its comparison operators overloaded if ( *B.get() <...

reference counting with cycles in C++ smart pointer

Hi In shared_ptr smart pointer, reference counting is used. However, reference counting has a problem, that it can't break cycles of reference. I have four questions with this issue. 1) Could anybody offer me one snippet in which the cycles of reference happened? 2) If it can't break cycles of reference, how does RCSP guarantee succ...

Why is there no boost::copy_on_write_ptr?

I just saw this nice copy-on-write pointer implementation. It looks pretty generic and useful, so my question is: Is such a class contained in any of the C++ toolkits (boost, loki, etc.)? If not, I'd really like to know why because it is a really useful idiom and apparently a generic implementation seems doable (like the one I linked to)...

why weak_ptr can break cyclic reference?

I learnt a lot about weak_ptr working with share_ptr to break cyclic reference. How does it work? How to use that? Can any body give me an example? I am totally lost here. One more question, what's a strong pointer? ...

Is auto_ptr deprecated?

Will auto_ptr be deprecated in incoming C++ standard? Should unique_ptr be used for ownership transfer instead of shared_ptr? If unique_ptr is not in the standard, then do I need to use shared_ptr instead? ...

What is the correct way to make sure user use shared_ptr and not regular ptr?

In my class the constructor is private and I added a static method "CreateMyClassPtr" that uses the constructor and returns a share_ptr of it. Is it the correct implementation? Do you think I even have to make sure that shared_ptr will be used? Should I maybe leave it to the user to decide? ...

Why implement DB connection pointer object as a reference counting pointer? (C++)

At our company one of the core C++ classes (Database connection pointer) is implemented as a reference counting pointer. To be clear, the objects are NOT DB connections themselves, but pointers to a DB connection object. The library is very old, and nobody who designed is around anymore. So far, nether I, nor any C++ experts in the co...

Can i pass auto_ptr by reference to functions?

is the following function OK: void DoSomething(auto_ptr< … >& a).... ...

How to use references, avoid header bloat, and delay initialization?

I was browsing for an alternative to using so many shared_ptrs, and found an excellent reply in a comment section: Do you really need shared ownership? If you stop and think for a few minutes, I'm sure you can pinpoint one owner of the object, and a number of users of it, that will only ever use it during the owner's lifeti...

How to include only BOOST smart pointer codes into a project?

What are best practices to include boost smart pointer library only without adding all boost libraries into the project? I only want boost smart pointer library in my project and I don't want to check in/commit 200 MB source codes (boost 1.42.0) into my project repository just for that. What more, my windows mobile project itself doesn'...

Did anyone give these smart pointers (auto_any, scoped_any, shared_any) a test drive?

I'm investigating smart pointers with "shared" functionality for Windows CE and Mobile, where the VS 2008 tr1 std::shared_ptr cannot be used (due to linkage to a v.9 dll not present on CE, obviously, if I understand it correctly). There's a semi-old MSDN Magazine article with sources from a Microsoftie (Eric Niebler): Achieve More Relia...

Remove from a std::set<shared_ptr<T>> by T*

I have a set of shared pointers: std::set<boost::shared_ptr<T>> set; And a pointer: T* p; I would like to efficiently remove the element of set equal to p, but I can't do this with any of the members of set, or any of the standard algorithms, since T* is a completely different type to boost::shared_ptr<T>. A few approaches I can t...