Archive for January, 2010

How to define template functions/classes in different files

January 27th, 2010

Here is a long discussion:

In a word, the simplest way in practice is to put the definition (which at the same time is delearation) of the template functions/classes only in the .h header file, but not to separate the definition into a .cpp file.

Also see:

How many elements should be set in vector when using set_intersection?

January 27th, 2010

I tried this example on this page: which is listed below:

// set_intersection example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main () {
  int first[] = {5,10,15,20,25};
  int second[] = {50,40,30,20,10};
  vector<int> v(10);                           // 0  0  0  0  0  0  0  0  0  0
  vector<int>::iterator it;

  sort (first,first+5);     //  5 10 15 20 25
  sort (second,second+5);   // 10 20 30 40 50

  it=set_intersection (first, first+5, second, second+5, v.begin());
                                               // 10 20 0  0  0  0  0  0  0  0

  cout << "intersection has " << int(it - v.begin()) << " elements.n";

  return 0;

I also tried to change the vector’s initial length to 1, 2, 20. All to them worked fine. However, when I tried this (with vector’s initial length set to 10) on two sets which have 49 common objects. The memory leaked. So in practice, how large do we need to set the vector?

using self-defined comparator with binary search in C++

January 22nd, 2010

For example, you want std::pairs, which normally compare with their first values (the keys),  to compare with their second values. You could write a comparator yourself and use it to compare the pairs.

bool PairCompF (const std::pair<node_t, result_t> &lhs, const std::pair<node_t, result_t> &rhs) {
return lhs.second < rhs.second;

int main(int argc, char* argv[]) {

pair<int, int> a(12, 44);
pair<int, int> b(23, 30);
pair<int, int> c(30, 23);
pair<int, int> d(40, 23);
pair<int, int> e(50, 10);
vector< pair<int, int> > v;

cout << “lower bound of 23 is ” << (lower_bound(v.begin(), v.end(), c, PairCompF) – v.begin()) << endl;
cout << “upper bound of 23 is ” << (upper_bound(v.begin(), v.end(), c, PairCompF) – v.begin()) << endl;

return 0;


How to set up SVN server on Ubuntu

January 20th, 2010

An excellent instruction: It tells you how to set up the svn server via svn protocol and make the sever run at startup. Note, when setting up the initialization script, you may need to install initscripts first by:

sudo apt-get install initscripts

Otherwise, you may get an error like this: /lib/init/ No such file or directory …

More detail:

Zend Framework (>=1.8) Step By Step

January 18th, 2010


  • environment set-up: eclipse / pdt / xDebug
  • quick start: /
  • zend application
  • db
  • auth / acl
  • router
  • session
  • module
  • ajax
  • cache
  • captcha / file
  • mail
  • test
  • performance
  • server clusters

Books and references:

Zend_Session_SaveHandler_DbTable doen’t work

January 15th, 2010

My current ZF version is 1.9.4. However, because my application started from 1.7.x, I wrote all bootstrap code in index.php. So we config Zend_Session_SaveHandler_DbTable like this as the document said in index.php:

$config = array(
‘name’                 => ‘session’,
‘primary’              => ‘id’,
‘modifiedColumn’  => ‘modified’,
‘dataColumn’        => ‘data’,
‘lifetimeColumn’    => ‘lifetime’
$saveHandler = new Zend_Session_SaveHandler_DbTable($config);

And in indexAction of IndexController, I tried to test it like this:

$session = new Zend_Session_Namespace();
$session->name = “newsbag”;

But nothing happened. There was no data inserted into the session table.

But later, I create a totally new blank project following the Quick Start of ZF & Tutorial: Getting Started with Zend Framework 1.9, it worked!

Still trying to figure out what the problem…

========== update 1 ============

Now the problem seems to be caused by the Zend_Controller_Front instance in index.php. Because when I tried to add self-defined routers in index.php in the blank project mentioned above like this:

require_once ‘Zend/Controller/Router/Route.php’;
$config = new Zend_Config_Ini (‘../configs.application.ini’, ‘routers’);
$router = new Zend_Controller_Router_Rewrite();

the Zend_Session_SaveHandler_DbTable refused to work.

So DON”T instance Zend_Controller_Front in index.php.

BTW, the possible way to set routers in ZF (>= 1.8.0) would be in Bootstrap.php:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
function _initRouter(array $options = null) {
$frontController = $this->getResource(‘FrontController’);
$config = new Zend_Config_Ini (‘../application/configs/application.ini’, ‘routers’);
$router = new Zend_Controller_Router_Rewrite();

Self-defending applications?

January 14th, 2010

Have you ever thought that your application would defend malwares itself? That’s what a group from OWASP is doing. They are developing a document (the name is AppSensor) which suggests us how to build applications that can detect and response to the attack real time. Sounds interesting, isn’t it?

Here is their website: And they have released the AppSensor Beta Release 1.1. We can download the PDF version or the DOC version now.

ways to build session server (cluster)

January 13th, 2010

There are many ways to build centre session server.

Using LVS to build web server cluster

January 13th, 2010

LVS (Linux Virtual Server) is a common used application to build virtual server systems on Linux OS. Here we use it to build a web server cluster under Ubuntu.

The first step is to install ipvsadm on your load balancer:

$ sudo apt-get install ipvsadm

Then we can use ipvsadm to configure our virtual server system. Suppose our load balancer is134.21.2.1, and the real web servers are:


We use these commands to add the service and the servers:

$ sudo ipvsadm -A -t -s wlc                                            # add the service on the load balancer
$ sudo ipvsadm -a -t -r -m -w 100                  # add real web server No. 1 to the service
$ sudo ipvsadm -a -t -r -m -w 100          # add real web server No. 2 to the service
$ sudo ipvsadm -a -t -r -m -w 100          # add real web server No. 3 to the service

We also can use ipvsadim -d to delete a real server, for example:

$ sudo ipvsadm -d -t -r

More details please go to LVS Documents, or check the manual:

$ man ipvsadmin

MTL 4 Installation

January 6th, 2010

The installation of MTL 4 is extremely easy. Actually, we only need to download 2 packages (boost and mtl4) and extract them locally if we don’t want to run the tests and example programs. The complete installation guide please you could always refer to the official site:

  • The first step is to download and extract boost which is used by MTL 4. Go to boost’s homepage, and download the newest version of boost. Suppose you save it on your local path as /home/me/programs/. Extract it as boost_x_xx_xx, such as boost_1_41_0. So your boost folder is /home/me/programs/boost_x_xx_xx, now.
  • The second and the final step is to download and extract mtl4. Go to, choose the latest version and download it to, for example, /home/me/programs. Extract it. Now your mtl folder would be /home/me/programs/mtl4.

Now it’s time to use the mtl library. You can include whatever you want like this:

#include <boost/numeric/mtl/mtl.hpp>

And compile your program like this (suppose you are using the GNU C++ compiler):

g++ -I/home/me/programs/mtl4 -I/home/me/programs/boost_x_xx_xx -O2 vector1.cpp -o vector1

Please refer to to see a detail Hello World program.

If you don’t like to include the boost path every time you compile your program, then you can install it as a binary library on your machine. You can always check the offical Boost Get Started. And here is a instruction about installing boost on Gentoo and Debian/Ubuntu: Installing C++ Boost on Gentoo and Debian/Ubuntu.