Decent shared_ptr implementation that does not require a massive library?
-
19-09-2019 - |
Question
I am taking a C++ programming class right now on GIS Programming. I am really starting to get alot of headaches from dealing with proper memory management. Considering at any time there is often 8-10 classes each holding a pointer to a 3D matrix or something else very large. Now our class already raised the issue of the prof allowing us to use Boost, or atleast the C++ Feature Pack for 2008(for TR1). He refused but said if we wanted to we can find add a few third party cpp/hpp files. I already tried looking at getting shared_ptr out of boost but that is more of a headache than its worth.
So is there any sort of free shared_ptr implementation out there?
Solution
Use boost's bcp tool. It will let you extract certain functionality from the boost libraries.
bcp shared_ptr /boost_shared_ptr
will extract shared_ptr and it's dependencies to that directory.
OTHER TIPS
Give Lokis ref-counted smart pointer a shot - as far as i recall its less coupled then boosts headers.
Preprocess boost header that contains the definition of shared_ptr
. Write it to a single .hpp file. This way you'll get boost shared_ptr
and all its dependencies in one header file, without the need for full installation of boost.
shared_ptr
does not need any shared library to be linked to your code, it is a header-only library... so this should work.
While it would be a terrible idea for a production solution, it wouldn't be too hard to roll your own for a class, if you didn't try to be as cross compiler, flexible and thread safe as boost's:
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_;
};
#include <tr1/memory> // this is contained in STL.
std::tr1::shared_ptr<A> a = new A;
ow, just saw your professor doesnt allow you to use TR1. tough luck.
Do you really need shared ownership that much?
You can often get by with just a simple ad hoc RAII class, taking exclusive ownership of an object.
Memory management without RAII is a pain, but you an get RAII without shared_ptr
.
I just spotted a serious mistake in your code sample. It should be
ref_count_(p ? new int(0) : NULL)
instead of
ref_count_(p ? new int : NULL)
the counter should be initialized to 0. if not all your smart pointer is not smart anymore. All those programmers that do those tiny mistake pay for it a lot in debugging later, you should think like a compiler and act like it....