Wednesday, January 7, 2009

static variables in a C++ class

Certainly mature methods of software coding recommend that you avoid global variables. So sometimes you end up using static data members within C++.

I recently learned this about using static variables in C++ classes. You need to define the static data member outside the class declaration. Failure to do this results in a linker error, the reason being because without the definition the compiler doesn't know which translation unit (hence object file) the member is supposed to go.

Sample code:
foo.h

class bar {
public:
int getBar1();
private:
static int bar1;
};

foo.cpp
#include "foo.h"

int bar::bar1;
.....
....

One more reason why I don't consider myself a C++ programmer.

Flying Linux

As I am getting ready for a flight this weekend on Virgin America, I remembered something I read a few months back.

The in-flight entertainment system on Virgin America, RED runs on Fedora/Red Hat Linux. Very cool.

Here is a good interview with Charles Ogilvie, the Director of In-flight Entertainment and designer of RED. I am certainly looking forward to checking it out. At least there are still one or two good reasons to fly.

Sunday, September 7, 2008

once you go penguin, you don't go back

I was having a conversation with a colleague today who switched to Ubuntu a few months back and has not had to "boot into Windows since". Good deal, very much the kind of experience most people have once they start using Ubuntu. I have not yet switched to 8.10, but my experience with 8.04 has been exceptional. I still dual boot, but don't remember the last time I booted Windows.

And since I am Windows b*tching, I can't believe that Dell charges something close to $160 if you are purchasing and customizing your spanking new system and want XP instead of Vista. Because nothing says freedom of choice like having to pay extra for an older version of a software.

Not necessarily on topic, but had this link bookmarked about best free (not open source) software to try out.

Friday, August 8, 2008

manually mounting the windows partition

I had to manually mount my windows partition the other day because after a reboot, it did not get auto mounted.

sudo mount -t ntfs -o nls=utf8,umask=0222 /dev/hdb1 /media/c

Monday, July 7, 2008

inotify

I recently stumbled upon inotify. A set of calls to monitor file system events on an individual file or a directories.

Used in combination with read(), you can read an event from an inotify file descriptor that is "watching" files or directories. On a successful event, read() returns a buffer containing an inotify_event.

checkout man inotify for more information. IBM's DeveloperWorks has a good writeup on it here.

Tuesday, June 3, 2008

cool Firefox add-on


I am still struggling to make my peace with Firefox 3-beta recently...

But a new issue that has bothered me enough that I have had to switch to using IE on the Windows XP system at work, is that pages with lots of Flash or Java script content takes way too long to load and the browser completely freezes another 30 seconds - 1 minute after that.

I have tried the obvious things:
  • disable all add-ons
  • uninstall all add-ons
  • uninstall/reinstall Firefox
Switched back to using 2.0.0.14 which got me back all my Add-ons, but didn't still rid me of the problem.

Anyways, in the frustration, I stumbled onto a damn cool add-on call Tab Effect. Its a "Ubuntu desktop-effects-cube-inspired" transition between multiple tabs. And as expected, it is still not available for Firefox 3.

Oh well, I still need to find a solution to my Firefox woes.

header guard

When working on large software development projects, you often include header files from various parts of the source repository and a cool trick to avoid redefinitions is by use of "header guard".

The concept is fairly simple. Consider the following header files:
foo.h
-----
typedef struct {
int x;
} foo;


bar.h
-----
#include "foo.h"
typedef struct {
foo f;
} bar;


Now when you include these files as follows:
mainfile.c
----------
#include "foo.h"
#include "bar.h"
.....
.....


Obviously you will run into a "redefinition" error when compiled.

The solution:
Place
#ifndef /* unique symbol is generally some form of the file name */
#define
at the beginning of the header file, and
#endif
at the end of the header file.

What this does is that the first time the unique symbol is encountered, the symbol gets defined and none of the definitions in the header file are then redefined.

So foo.h would look like:
#ifndef _FOO_H_
#define _FOO_H_
typedef struct {
int x;
} foo;
#endif /* _FOO_H_ */


and bar.h is like:
#ifndef _BAR_H_
#define _BAR_H_
#include "foo.h"
typedef struct {
foo f;
} bar;
#endif /* _BAR_H_ */