Extract the sources to some folder, open the command prompt and go to that directory.
Then enter following commands…
> bootstrap.sh > bjam.exe --toolset=msvc --link=static --runtime-link=static --build-type=complete stage
Extract the sources to some folder, open the command prompt and go to that directory.
Then enter following commands…
> bootstrap.sh > bjam.exe --toolset=msvc --link=static --runtime-link=static --build-type=complete stage
Boost::format’s performance sucks.
I used boost::format in an inner loop in my project, and the optimized runtime was 20 seconds for 1 small case.
I optimized away boost::format by replacing it with stream manipulators and i got the runtime down to 2 seconds for the same case.
I’ll publish an article later on the details on how to do this.
I always forget how to use boost::format when I need it. So, here are a few samples from when I did remember.
Only need is to include the proper boost header. No compile of any of the boost libraries is necessary.
The source is also available in subversion (webclient) (subversion checkout).
// build with make or:
//
// g++ -I../../boost_1_46_0 fmt.cpp
//
#include <iostream>
#include <sstream>
#include <boost/format.hpp>
using namespace std;
using namespace boost;
int main()
{
stringstream ss;
int year = 2011;
int month = 3;
int day = 11;
int hour = 12;
int minute = 1;
double seconds= 23.35;
double epoch = 1231234567890.35;
// Fill with whitespace, right align
ss << format("#1.0 %4i %4i %4i\n") % year % month % day;
// Output: #1.0 2011 3 11
// Fill with whitespace, left align
ss << format("#1.1 %-4i %-4i %-4i\n") % year % month % day;
// Output: #1.1 2011 3 11
// Fill with zero (pad)
ss << format("#2.0 %04i %04i %04i\n") % year % month % day;
// Output: #2.0 2011 0003 0011
// Floating point precision
ss << format("#3.0 %11.2f\n") % seconds;
// Output: #3.0 23.35
// Floating point precision with zero-fill (zero pad)
ss << format("#4.0 %011.2f\n") % seconds;
// Output: #4.0 00000023.35
// Left aligned string literal with 30+20 columns
ss << format("#5.0 %-30s%-20s\n") % "LEFT ALIGNED 1" % "LEFT ALIGNED 2";
// Output: #5.0 LEFT ALIGNED 1 LEFT ALIGNED 2
// Right aligned string literal
ss << format("#6.0 %30s%20s\n") % "RIGHT ALIGNED 1" % "RIGHT ALIGNED 2";
// Output: #6.0 RIGHT ALIGNED 1 RIGHT ALIGNED 2
cout << ss.str();
return 0;
}
The output should be:
#1.0 2011 3 11 #1.1 2011 3 11 #2.0 2011 0003 0011 #3.0 23.35 #4.0 00000023.35 #5.0 LEFT ALIGNED 1 LEFT ALIGNED 2 #6.0 RIGHT ALIGNED 1 RIGHT ALIGNED 2
When porting projects from Visual Studio 2008 or earlier to Visual Studio 2010, and you’re using boost::shared_ptr or other features present in TR1, there will be conflicts. Boost has their own implementation of shared_ptr in boost::shared_ptr and with VS2010 they have put shared_ptr in std::tr1 and they have made shared_ptr available through std::shared_ptr. When using using namespace std; and using namespace boost; there will be ambiguities.
To disable the C++0x/TR1 headers from VS2010 and use the boost implementation, define _HAS_CPP0X=0 in the preprocessor project settings for your VS2010 project.
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.
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.
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.
Building Boost on Windows is a bit harder than on Linux, but it’s not rocket science
.
/*
* 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.
/*
* 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!