Moving away from WordPress

I will be blogging on from now onwards. Please update your RSS readers to point to the new location.


Simple, generic and dynamically allocated array in C

C is a very good language. I have been using for quite some time for my opensource project. The flexibility C offers is really good. But sometimes, lack of simple datastructures like a dynamically growing array will slow down the programmer. There are tons of implementation available online, but most of them are overcomplicated, got lot of dependencies or tough to understand and incorporate with your application. In this post, I present a simple array which grows dynamically, reuses the memory, supports any pointer type and easy to copy to your code base.

Here is the code.

typedef struct varray_t
   void **memory;
   size_t allocated;
   size_t used;
   int index;
} varray;

varray_init(varray **array)
   *array = (varray*) malloc (sizeof(varray));
   (*array)->memory = NULL;
   (*array)->allocated = 0;
   (*array)->used = 0;
   (*array)->index = -1;

varray_push(varray *array, void *data)
   size_t toallocate;
   size_t size = sizeof(void*);
   if ((array->allocated - array->used) < size) {
      toallocate = array->allocated == 0 ? size : (array->allocated * 2);
      array->memory = realloc(array->memory, toallocate);
      array->allocated = toallocate;

   array->memory[++array->index] = data;
   array->used = array->used + size;

varray_length(varray *array)
   return array->index + 1;

varray_clear(varray *array)
   int i;
   for(i = 0; i < varray_length(array); i++)
      array->memory[i] = NULL;
   array->used = 0;
   array->index = -1;

varray_free(varray *array)

varray_get(varray *array, int index)
   if (index < 0 || index > array->index)
      return NULL;

   return array->memory[index];

varray_insert(varray *array, int index, void *data)
   if (index < 0 || index > array->index)

   array->memory[index] = data;

This array doesn’t take ownership of the data it contains. Ownership has to be managed separately. When adding a new item, array grows in constant propotion yielding inserts in amortized constant time. I have omitted error checking for malloc and realloc for simplicity.

Hope you enjoy the post.

Simple egg timer on Linux for Pomodoro technique

Recently I have started evaluating the Pomodoro Technique which I found quite interesting. I am still evaluating the technique and not yet concluded on whether I should continue using it.

To implement pomodoro technique, you need a kitchen timer or egg timer. Since I use pomodoro for my programming work, I obviously don’t want to use a physical timer. I couldn’t find a decent timer for linux especially one that works well with Xfce. In this blog post, I will explain building a kitchen timer with basic linux programming techniques. This also gives an idea about how simple linux tools can be combined to do useful stuff.

Things that are used

  1. Shell scripting
  2. Notification mechanisms on popular desktops like Gnome and Xfce. (I use Xfce in this example)

The shell script is actually a modified version of the one published here (

notify-send -u critical -i appointment -t 600 "$summary" "$startmessage"
while [ $counter != $limit ]; do
   echo "$counter minutes so far...";
   sleep 60
   let "counter = $counter + 1"
if [ $counter = $limit ]; then
   notify-send -u critical -i appointment "$summary" "$endmessage"
   echo -e '\a' &gt;&amp;2
   exit 0

All it does is wait until the limit reaches. It uses the sleep(1) command to sleep for a minute. notify-send is used for sending notifications to the desktop environment.

This script can be invoked using,


If you use bash, you can add an alias for convenience.

alias begin-pomodoro='sh ~/utils/ 25 "Pomodoro" "Pomodoro started, you have 25 minutes left" "Pomodoro ended. Please stop the work and take short break"'

Pomodoro started notification

Pomodoro ended notification

This post is written on one pomodoro!

Happy programming!

Unit testing events – Asserting event rasing

I was working on a Silverlight application where property setter raises PropetyChanged events for other read only properties that needs to be updated. Something like,

private List<User> users;
public List<User> Users
           return users;
           users = value;

private void Changed(string propertyName)
      if (PropertyChanged == null) return;
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

Here is how I came up with a test case that asserts the proper events are raised in proper order.

public void ProperEventsAreRaisedWhenUsersChanges()
       var propertiesChanged = new List<string>();
       var model = new UserModel();
       model.PropertyChanged += (sender, args) => propertiesChanged.Add(args.PropertyName);
       model.Users = new List<User>();

ShouldBe() is an extension method which does Assert.That internally.

Invoking root directorie’s makefile from subdirectory in EMACS

To compile, M-x compile is used in emacs. The value displayed in the mini-buffer for this command is stored in the compile-command variable. When you execute, make -k, emacs executes the command in the directory on which the currently opened file belongs. This won’t help as you may not have makefile in the current directory.

Most of the projects will have only one makefile which will be available in the root directory. To invoke this makefile from a subdirectory, you can use -f switch and specify the makefile path. Something like:

make -f project_root_directory/Makefile

Still, emacs executes this command from the current directory. This method has got potential problems like relative paths used inside the makefile won’t work. A reliable method would be to ask emacs to change the current directory to project’s root, execute the makefile and come back to the current directory. Fortunately, emacs supports cd command which can be used to change the directory and cd – to come back to last directory. After issuing the compile command (M-x compile), you can type

(cd /project_root_directory && make && cd -)

Using this method, emacs executes the makefile from the root directory. You can add the following to .emacs file if you need this trick always.

(setq compile-command "(cd /project_root_directory && make && cd -)")

Happy programming!

How overriding Object.ToString() helps

System.Object provides a ToString() method which gives a human readable string about the object. Many people haven’t realized how useful is this tiny method.

Consider the following code:

namespace PersonDetails
    class Person
        public Person(string firstName, string lastName, int age)
            this.FirstName = firstName;
            this.LastName = lastName;
            this.Age = age;

        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }

    class Program
        static void Main(string[] args)
            Person chuckNorris = new Person("Chuck", "Norris", 20);
            Console.WriteLine(chuckNorris.ToString()); // Prints PersonDetails.Person

The information printed is not at all helpful. Let us override ToString() and see the difference. Continue reading

Understanding equality and object comparison in .NET framework

.NET framework provides several methods to do comparison and equality check for objects. It can be pretty confusing at the beginning by looking at the documentations for each interfaces that framework offers. In this post, I will try explain the possibilities with examples.

Basically, there are two kinds of equality in .NET. Reference Equality and Value Equality. Default is reference equality. To understand reference equality, consider the following class: Continue reading