Boost Smart Pointers or: How I learned to Stop Worrying and Love C++

Boost smart pointers fix many of the things wrong with auto_ptr. The smart pointer library consists of

scoped_ptr <boost/scoped_ptr.hpp> Simple sole ownership of single objects. Noncopyable.
scoped_array <boost/scoped_array.hpp> Simple sole ownership of arrays. Noncopyable.
shared_ptr <boost/shared_ptr.hpp> Object ownership shared among multiple pointers
shared_array <boost/shared_array.hpp> Array ownership shared among multiple pointers.
weak_ptr <boost/weak_ptr.hpp> Non-owning observers of an object owned by shared_ptr.
intrusive_ptr <boost/intrusive_ptr.hpp> Shared ownership of objects with an embedded reference count.

The table is courtesy of the link here.

I will just go through a simple example here. For a slightly more detailed look, you can take a look at this article.

Installing Boost on Mac OS X

Compiling and installing Boost is straightforward even if it takes some time and is processor intensive–it kept both cores of my Intel Core Duo pretty busy. What you really should do is follow the more authoritative guide from the Boost folks here. Here is my summary. Disclaimer: This is not a comprehensive guide. I am not liable for any failures you might have. See the link above for a more detailed guide.

  1. Install bjam (it is a tool like make, but supposedly better). Put bjam in your PATH
  2. Invoke bjam. Step 3 shows what an invocation might look like(you can customize this to your preferences): The following command works if you have the boost_1_34_0 directory in ~/ and bjam built and set up correctly.
  3. $ cd ~/boost_1_34_0
  4. $ bjam --build-dir=/tmp/build-boost --toolset=darwin stage
  5. After a while, type in
    echo $?

    to make sure everything proceeded correctly.

  6. Try the example given on the Boost page(see here).

A smart pointer example.

I’ll be using the IT++ library for this example. Of course, you can make your own examples without the library. The concepts are what matter.

typedef boost::scoped_ptr<PAM> pam;

int main()
pam p(new PAM(2));

vec output;

bvec demodded_out;

bvec input = “0100100001100101011011000110110001101111001000000101011101101111011


//The following line of code simulates the transmitter end.

//The following simulates the receiver end.
if(demodded_out == input)
else {
return 0;

This is basically my previous code, with some smart pointer stuff added in. Notice that I didn’t deallocate any memory(I didn’t have to).


One response to “Boost Smart Pointers or: How I learned to Stop Worrying and Love C++

  1. Pingback: C++ for ObjC Programmers Part III: Smart Pointers « Mac OS X coding

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s