A decent implementation of shared_ptr that does not require a massive library?

I am taking the C ++ programming class right now on GIS programming. I am really starting to get a lot of headaches from proper memory management. Given that at any time there are often 8-10 classes, each of which contains a pointer to a 3D matrix or something else very large. Now our class has already raised the issue of a profile that allows us to use Boost, or at least the C ++ Feature Pack for 2008 (for TR1). He refused, but said that if we want us to be able to add several third-party cpp / hpp files. I already tried looking at getting shared_ptr because of the boost, but this is more of a headache than its value.

So, is there any free implementation of share_ptr?

+6
c ++ shared-ptr
source share
7 answers

Use boost bcp . This will allow you to extract certain functionality from boost libraries.

bcp shared_ptr /boost_shared_ptr 

will retrieve shared_ptr and its dependencies to this directory.

+17
source share

Give the Lokis a ref-counted smart pointer a shot - as far as I remember it is less connected, and then raises the headlines.

+4
source share

A preprocessor header header containing the definition of shared_ptr . Write it in a single .hpp file. Thus, you will get boost shared_ptr and all its dependencies in one header file, without the need to fully install boost.

shared_ptr does not require any shared library to bind to your code, it is a header-only library ... so this should work.

+3
source share

Although it would be a terrible idea for a production solution, it would not be too difficult to roll on your own for the class if you were not trying to be cross-compiler, flexible and thread safe as boost:

 template <typename contained> class my_shared_ptr { public: my_shared_ptr() : ptr_(NULL), ref_count_(NULL) { } my_shared_ptr(contained * p) : ptr_(p), ref_count_(p ? new int : NULL) { inc_ref(); } my_shared_ptr(const my_shared_ptr& rhs) : ptr_(rhs.p), ref_count_(rhs.ref_count_) { inc_ref(); } ~my_shared_ptr() { if(ref_count_ && 0 == dec_ref()) { delete ptr_; delete ref_count_; } } contained * get() { return ptr_; } const contained * get() const { return ptr_; } void swap(my_shared_ptr& rhs) // throw() { std::swap(p, rhs.p); std::swap(ref_count_, rhs.ref_count_); } my_shared_ptr& operator=(const my_shared_ptr& rhs) { my_shared_ptr tmp(rhs); this->swap(tmp); return *this; } // operator->, operator*, operator void*, use_count private: void inc_ref() { if(ref_count_) { ++(*ref_count_); } } int dec_ref() { return --(*ref_count_); } contained * ptr_; int * ref_count_; }; 
+1
source share
 #include <tr1/memory> // this is contained in STL. std::tr1::shared_ptr<A> a = new A; 

ow, I just saw that your professor does not allow you to use TR1. hard luck.

+1
source share

Do you really need a joint ownership?

You can often walk through the simple ad hoc RAII class, excluding ownership of the object.

Managing memory without RAII is a pain, but you get RAII without shared_ptr .

+1
source share

I just noticed a serious error in your code example. It should be

 ref_count_(p ? new int(0) : NULL) 

instead

 ref_count_(p ? new int : NULL) 

the counter should be initialized to 0. if not all of your smart pointers are no longer smart. All those programmers who make this tiny mistake pay a lot for debugging later, you should think like a compiler, and do so ...

+1
source share

All Articles