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 ? ...
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 ? ...
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...
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...
#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...
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. ...
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? ...
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<...
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...
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() <...
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...
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)...
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? ...
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? ...
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? ...
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...
is the following function OK: void DoSomething(auto_ptr< … >& a).... ...
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...
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'...
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...
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...