Posts Tagged ‘C++’

c++ arrays memo

November 30th, 2010

1. arrays initialization

under GCC, when you create an array statically, it’s not initialized. while you create it dynamically, it’s initialized.
here is the test code:

int main () {

	int N(1000000);

	// statically created array, not initialized
	int a[N];
	for(int i = 0; i < N; ++i) {
		if(a[i] != 0) {
			cout << "a[N] is not all zero!!!" << endl;

	// dynamically created array, initialized
	int *b;
	b = new int[N];
	for(int i = 0; i < N; ++i) {
		if(b[i] != 0) {
			cout << "b[N] is not all zero!!!" << endl;
	// dynamically created array, initialized	
	int *c;
	c = new int[N]();
	for(int i = 0; i < N; ++i) {
		if(c[i] != 0) {
			cout << "c[N] is not all zero!!!" << endl;

2. return array from function

Arrays of other types are typically returned via reference:

void retArray( (int*)& p, unsigned& size )
	size = 4;
	p = new int[ size ];
	for (int n = 1; n < 4; n++)
		p[ n ] = n;

C++ constructor

May 30th, 2010

C++ derived classed will call parent’s default constructor by default at the beginning of construction. Note, only at the beginning. It’s not like Java, you can call super() at the any time you want.

And if you want to call a parent constructor with parameters (of course, also at the beginning of the construction), you can do it like this:

Derived(int ap, int bp) : Base(ap, bp) {/* do something you want in the derived class constructor */}

The destructor will also call the parent destructor.

Here is a example:

#include <iostream>

using namespace std;

class Base {
int a;
int b;

Base() {
cout << “base constructor” << endl;

Base(int ap, int bp): a(ap) {
b = bp;
cout << “base constructor with param” << endl;

~Base() {
cout << “base destrctor. a: ” << a << “, b: ” << b << endl;

class Derived : public Base {
Derived() {
cout << “derived constructor” << endl;

Derived(int ap, int bp) : Base(ap, bp) {
cout << “Derived constructor with param” << endl;

~Derived() {
cout << “derived destrctor. a: ” << a << “, b: ” << b << endl;

int main() {
Base b(3, 4);
Derived d(3, 4);

c++ vector memory taken

March 2nd, 2010

Here is a experiment about how much memory will a vector array take with different size on a 64bit machine with a 64bit OS (Ubuntu).

Vector Arr Size   Memory
10K * 1             208K
10K * 10           688K
10K * 100         5.4M
10K * 1000       52.8M
12K * 100         12.3M
100K * 10         10M
300K * 10         30M
300K * 100        158M

Note: 10K * 1 means an array of 10K vectors with each vector having 1 int element.

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;


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.

ATLAS installation memo

January 5th, 2010

Tried to install ATLAS 3.9.20 according to the instruction: doc/atlas_install.pdf.

As said, the simplest way is:

bunzip2 -c atlas3.9.x.tar.bz2 | tar xfm -    # create SRCdir
mv ATLAS ATLAS3.9.x                            # get unique dir name
cd ATLAS3.9.x                                       # enter SRCdir
mkdir Linux_C2D64SSE3                         # create BLDdir
cd Linux_C2D64SSE3                              # enter BLDdir
../configure -b 64 -D c -DPentiumCPS=2400 # configure command
–prefix=/home/whaley/lib/atlas          # install dir
make build                                     # tune & build lib
make check                                    # sanity check correct answer
make time                                      # check if lib is fast
make install                                   # copy libs to install dir

There are some things to be noticed:

  • remember to change the options for configure. For example, your CPU info can be found out by $cat /proc/cpuinfo;
  • make sure fort77 is installed;
  • make sure gfortran is installed otherwise an error “gcc: error trying to exec ‘f951′: execvp: No such file or directorygcc” would be reported.