Tuesday, July 1, 2008

Blitz++

Blitz++ is a C++ class library for scientific computing, and it's aim is to give performance similar to that of Fortran 77/90.

I haven't tried it yet, but will try to get around to it soon...

C++ quick tutorial: const

The const keyword allows you to specify whether a particular variable can be modified or not.

The simplest usage of const is this, for example:

const int i = 8;

This just means that the variable i is always 8 and cannot be changed. An alternative way of writing this is:

int const i = 8;

which means that same thing. The value of the variable i cannot be changed. Clearly you must initialize constant variables when they are declared, as you can't change their value later! Using const in this context is an alternative to using #define, for example:

#define i 8

What about this example?

const int *px;

This basically means that *px is a const int, i.e. px is a pointer to a constant integer. The important thing to remember is that while *px can be pointed to any integer, you cannot change the value of the integer.

Here's another example:

int x;
int * const px = &x;

The difference here is that the const is after the *. The value x can be changed, however the address pointed to by px can't be changed.

Here's yet another example:

int x = 0;
const int * const px = &x;

In this case, we cannot change the value of x using the pointer px, and we cannot point px to another address.

Another usage is in passing parameters to functions. For example, firstly consider:

void myfunction(mystructure in);

Assume mystructure uses a large amount of memory. When myfunction is used, a copy of the structure is made which is available only inside the function. This may take a long time, and/or may use too much memory. Instead, we could pass the structure in by reference:

void myfunction(mystructure &in);

which means that inside the function we don't just have a copy of the mystructure variable, we have variable itself. This of course means that it is possible to modify this variable. If we want to ensure that it cannot be modified, we would make use of const:

void myfunction(mystructure const &in);

C++ quick tutorial: static

The static keyword has 3 different uses.

1. The simplest use is inside a function. If a function contains the following line:

static int count = 0;

the variable count will be set to zero only once, even if the function is called millions of times. For example, consider the following:

for (int i=0; i<1000; i++)
{
   static int count = 0;
   count++;
}

Here, the variable count is set to zero only once, not 1000 times.

2. If one file has at the top:

static float pi = 3.141592653;

then the global variable pi can be used inside just this one file. Other files are not able to access the variable pi. With just this:

float pi = 3.141592653;

then other files can access the variable pi provided they contain:

extern float pi;

3. static can also be used inside classes. For example:

class myclass
{
   int i;
}

If we then create two objects of this class:

myclass c1, c2;

then c1 and c2 will contain their own independent variable i. However, if instead we defined:

class myclass
{
   static int i;
}

and then created two objects:

myclass c1, c2;

there is only a single variable called i. Any instance of the class myclass has the same value of i. To set the value of i we can do this:

int myclass::i = 0;

The syntax is clearly of the form:

type class_name::static_variable = value

The thing here that confuses some people is that the type must be specified here as well.

C++ quick tutorial: typedef

Suppose you need to define lots of variables as unsigned short int, for example:

unsigned int a[3];

It can be useful in these situations to use typedef, which enables you to create aliases to any of the usual C++ datatypes. For example:

typdef unsigned int UnsignedInt;
...
UnsignedInt a[3];

In this example we have created an alias called UnsignedInt which we can then use to declare variables of type unsigned int.

Another example, this time using char:

typedef char[60] field;
...
field name;

Here we have created an alias called field which can be used to declare variables of type char[60].