Intro
A couple of months ago Boost came to me when I started in a new job just out of Uni, and I got impressed from day 1. Not only because in my previous experience Boost gave incredibly large errors[1] but what Boost really is and what the collection of libraries in Boost can do to easily avoid many of the pitfalls with C++ programming.
Obtaining Boost is easy, simple and free. Boost’s license explicitly states it’s free as in free beer, and you can use Boost in commercial applications without breaking the license agreement.
How to build Boost
Most libraries in Boost are header only, so you don’t have to build Boost to get started with it. However, a couple of the libraries requires to be built.
Linux
Building Boost under Linux is very easy. Supposing you have a working development environment on your computer, you’ll only have to do these steps to build Boost.
wget http://surfnet.dl.sourceforge.net/sourceforge/boost/boost_1_37_0.tar.gz tar zxvf boost_1_37_0.tar.gz cd boost_1_37_0 ./configure make
Then you’ll have a working Linux build of Boost.
Windows
Building Boost on Windows is a bit harder than on Linux, but it’s not rocket science
.
- Download and extract Boost to some folder.
- Download bjam.exe and put it in the root Boost folder.
- Invoke bjam with stage=complete + the options from the docs.
- Wait
- Now you should have a complete set of libraries.
Example 1
Memory leak
/*
* C++ example with a memory leak in an exception
*/
#include <iostream>
void throwErrorWithMemLeak()
{
try
{
// Allocate 1kB buffer
char *buffer = new char[1024];
// Throw
throw 1;
// Free up buffer
delete buffer;
}
catch(const int e)
{
// printf("We have a memory leak\n");
}
}
int main(int args, char **argc)
{
// A simple case of when an exception leads to a memory leak
// Iterate the throwing function 1024 times, creating a 1MB memeory leak
for (size_t i=0; i<1024; i++)
{
throwErrorWithMemLeak();
}
return 0;
}
Compile with
g++ -ggdb3 ex1_memleak.cpp -o ex1_memleak
And run it with valgrind to see the memory leak in action.
valgrind ./ex1_memleak --leak-check=full
==21831== Memcheck, a memory error detector.
==21831== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==21831== Using LibVEX rev 1854, a library for dynamic binary translation.
==21831== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==21831== Using valgrind-3.3.1, a dynamic binary instrumentation framework.
==21831== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==21831== For more details, rerun with: -v
==21831==
==21831==
==21831== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 1)
==21831== malloc/free: in use at exit: 1,048,576 bytes in 1,024 blocks.
==21831== malloc/free: 2,048 allocs, 1,024 frees, 1,167,360 bytes allocated.
==21831== For counts of detected errors, rerun with: -v
==21831== searching for pointers to 1,024 not-freed blocks.
==21831== checked 158,760 bytes.
==21831==
==21831== LEAK SUMMARY:
==21831== definitely lost: 1,048,576 bytes in 1,024 blocks.
==21831== possibly lost: 0 bytes in 0 blocks.
==21831== still reachable: 0 bytes in 0 blocks.
==21831== suppressed: 0 bytes in 0 blocks.
As you can see, we’ve lost 1 megabyte of memory into the void. If this were a long running program, even small memory leaks could be disastrous if it’s allowed to grow unlimited.
No memory leak
/*
* C++ example without a memory leak in an exception
*/
#include <iostream>
#include <boost/scoped_ptr.hpp>
void throwErrorWithMemLeak()
{
try
{
// Allocate 1kB buffer in an scoped pointer
boost::scoped_ptr<char> buffer( new char[1024] );
// Throw
throw 1;
// Free up buffer (we really don't need this)
// scoped_ptr.reset( 0 );
}
catch(const int e)
{
}
}
int main(int args, char **argc)
{
// A simple case of when an exception doesn't lead to a memory leak
// Iterate the throwing function 1024 times, creating a possible 1MB
// memory leak, but scoped_ptr prevents it gracefully.
for (size_t i=0; i<1024; i++)
{
throwErrorWithMemLeak();
}
return 0;
}
Compile with
g++ -ggdb3 -I../boost_1_37_0/ ex1_no_memleak.cpp -o ex1_no_memleak
Run with valgrind
valgrind ./ex1_no_memleak
==21902== Memcheck, a memory error detector.
==21902== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al.
==21902== Using LibVEX rev 1854, a library for dynamic binary translation.
==21902== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP.
==21902== Using valgrind-3.3.1, a dynamic binary instrumentation framework.
==21902== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al.
==21902== For more details, rerun with: -v
==21902==
==21902== ERROR SUMMARY: 1024 errors from 1 contexts (suppressed: 4 from 1)
==21902== malloc/free: in use at exit: 0 bytes in 0 blocks.
==21902== malloc/free: 2,048 allocs, 2,048 frees, 1,167,360 bytes allocated.
==21902== For counts of detected errors, rerun with: -v
==21902== All heap blocks were freed -- no leaks are possible.
No memory leaks!
This is only one small fraction of Boost, but yet a very powerful one. I’m in awe over Boost!