Tag Archives: Cplusplus

Building Boost library from source on Ubuntu

On Ubuntu, building the Boost library from source can be tricky at times. So, I thought of putting an article together to help someone trying the figure out the same. So, on your Ubuntu machine once you have downloaded the Boost source files (here), you can follow the following steps to build it (I have picked boost_1_54_0.tar.gz for demonstration) :

  1. Unzip and change into the boost source directory.
    tar -xf boost_1_54_0.tar.gz && cd boost_1_54_0
  2. Run bootstrap.sh, this will generate the necessary config files and “b2” (the build tool).
    ./bootstrap.sh
  3. Run b2 to build & install all (or desired) boost libraries (refer “./b2 –help” for explanation on individual options used):
    ./b2 --build-type=complete --layout=versioned --prefix=./install -q install

That’s all! The build (build-type=complete) would take a while to finish, so be patient. Also, the statndard Ubuntu installation might not have some headers required for the complete build. In my case, installing the following 2 packacges was sufficient :

  • libbz2-dev
  • python-dev

A more detailed set of instructions can be found here.

C++: Why prefer member initialization list over assignment?

The answer is simple – the latter could be a costly choice. I will try to explain this using a simple example. In the following program we have a class Bar that has a private data member of type Foo (another class). Now, lets say we want to create an object of type Bar initializing its data member with an object of type Foo, something like, Bar obj2(obj1); The following program defines the necessary functions to give an idea of what happens during the creation of such objects.

#include 

using namespace std;

class Foo {
public:
  Foo() { cout << "Foo: default ctor called." << endl; }
  Foo(const Foo &obj) { cout << "Foo: copy ctor called." << endl; }

  Foo & operator =(const Foo &obj)
  {
    cout << "Foo: overloaded assignment operator called." << endl;
    return *this;
  }

  ~Foo() { cout << "Foo: default dtor called." << endl; }
};

class Bar {
public:
  Bar() { cout << "Bar: default ctor called." << endl; }

  /* Parameterized constructor (without member initialization list) */
  Bar(Foo &foo)
  {
    cout << "Bar: ctor taking reference to Foo as param.." << endl;
    m_foo= foo;
  }

  ~Bar() { cout << "Bar: default dtor called." << endl; }

private:
  Foo m_foo;
};

int main()
{
  Foo obj1;
  Bar obj2(obj1);
  return 0;
}

Lets focus on Bar's parameterized constructor & how it affects the output.

..snip..

  /* Parameterized constructor (without member initialization list) */
  Bar(Foo &foo)
  {
    cout << "Bar: ctor taking reference to Foo as param.." << endl;
    m_foo= foo;                             /* Use assignment operator */
  }

..snip..

Output:
Foo: default ctor called.
Foo: default ctor called.
Bar: ctor taking reference to Foo as param..
Foo: overloaded assignment operator called.

Bar: default dtor called.
Foo: default dtor called.
Foo: default dtor called.

As we can see, while Bar's object is being constructed,

  1. Foo's default constructor is first invoked to construct the Foo's part of Bar, and then
  2. Foo's assignment operator is invoked to initialize it with the object supplied as parameter to this constructor.

Said that, let us now see how this can be improved by using member initialization list in Bar's parameterized constructor's definition.

..snip..

  /* Parameterized constructor (with member initialization list) */
  Bar(Foo &foo) : m_foo(foo)
  {
    cout << "Bar: ctor taking reference to Foo as param.." << endl;
  }

..snip..

Output:
Foo: default ctor called.
Foo: copy ctor called.
Bar: ctor taking reference to Foo as param..

Bar: default dtor called.
Foo: default dtor called.
Foo: default dtor called.

Looking at the above output, we can see that during the construction of Bar's object, Foo is being constructed & initialized directly by Foo's copy constructor, instead of using the default constructor to create an uninitialized object followed by the assignment operator.

Hence using member initialization list, one can completely avoid the use for overloaded assignment operator and thus save some precious CPU clocks.

Problem#1 : Multiply two numbers specified as strings

/**
  A function that multiplies two positive integer numbers stored as strings.

  @Assumptions:
    1) This program does not handle negative numbers. Not even +/- signs.
    2) It will not prune down leading 0's.
    3) The two input strings consist of valid digits only.

  @Algorithm:
    0) Create a result string of size (a.length + b.length).
    1) Take the last untouched digit (least-significant to start with) of one of
       the given numbers (in this case I picked 'b').
    2) Multiply it with all the digits of the other number ('a').
    3) Accumulate the outcome in the result string.
    4) Goto step 1.

*/

#include 

#define INT(c)  ((int)((c) - '0'))
#define CHAR(i) ((char)(i + int('0')))

using namespace std;

string multiply(const string &a, const string &b)
{
  /* initially fill the result string with 0's */
  size_t total_len= a.length() + b.length();
  string result(total_len, '0');

  int num;                                      /* intermediate store */
  int i, j;
  int last_pos_i, last_pos_j, last_pos_k;

  last_pos_i= total_len - 1;

  /* i-loop */
  for (i= b.length() - 1; i >= 0; i --)
  {
    last_pos_j= last_pos_i;

    /* j-loop */
    for (j= a.length() - 1; j >=0; j --)
    {
      last_pos_k= last_pos_j;
      num= INT(a.at(j)) * INT(b.at(i));

      /* k-loop : the actual updation of result string takes place here. */
      while (num)
      {
        num += INT(result.at(last_pos_k));
        result.at(last_pos_k)= CHAR(num % 10);
        /* 'carry' it forward.. ;) */
        num= num / 10;
        last_pos_k --;
      }

      last_pos_j --;
    }

    last_pos_i --;
  }
  return result;
}

int main()
{
  string n1("99");
  string n2("9");
  cout << n1 << " * " << n2 << " = "  << multiply(n1, n2) << endl;
  return 0;
}

Output:

> ./a.out 
99 * 9 = 891