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.
- Install bjam (it is a tool like make, but supposedly better). Put bjam in your PATH
- 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.
$ cd ~/boost_1_34_0
$ bjam --build-dir=/tmp/build-boost --toolset=darwin stage
- After a while, type in
to make sure everything proceeded correctly.
- 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;
pam p(new PAM(2));
bvec input = “0100100001100101011011000110110001101111001000000101011101101111011
//The following line of code simulates the transmitter end.
//The following simulates the receiver end.
if(demodded_out == input)
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).