At Dubai!

November 7, 2006 by Munir

So finally the day came when I crossed the boundaries of my homeland. Right now, I am chilling at Dubai International Airport, waiting for other MSPs from Islamabad, and writing this blog entry using high speed wireless internet. Still 4 hours remain for next connecting flight i.e. Dubai to Amsterdam.

Dubai International Airport is really a cool place. First time I am seeing so many people from different countries and cultures at one place. It is really a great experience for me!

Here are the few of pics I have taken:

Karachi International Airport Me at Karachi International Airport UAE From Sky

Entertainment Console in Emirates Dubai Duty Free 1 Dubai Duty Free 2

Visit here for more.

Beyond Developers Conference!

November 6, 2006 by Munir

Finally, I got the visa and I am ready to take-off for Barcelona on 7th November!

In fact the whole Microsoft Pakistan delegate got the Spanish visa but unfortunately one MSP is still waiting for luck. Even though only one day is left, we are hopeful that he will be with us.

There will be a private Microsoft Student Partners meeting on the evening of November 9th where 90+ Student Ambassadors from different countries will meet. After MSP meeting, we’ll enjoy Drinks & Buffet and play some games until midnight at Lasermon. I am so excited to attend this evening and to meet other MSPs!

“Ships are safe in harbors, but that’…

October 15, 2006 by Munir
Ships are safe in harbors, but that’s not what ships are for.

First blog entry using Google Docs!

Microsoft Tech-Ed Developer Conference 2006

October 8, 2006 by Munir

What would you do to get there?

I didn’t do such thing but I am one of four lucky Microsoft Student Partners (formerly Student Ambassadors) in Pakistan who got selected to fly Barcelona, Spain to attend Microsoft Tech-Ed Developers this year!

Barcelona, here we come!

Happy ending of a hectic week

August 27, 2006 by Munir

The whole last week was quite busy for me but I was able to take out time to participate in All Pakistan Dynamic Programming Competition at UTech 2006. Event went really good but since this was the first experience of UIT people to organize such events, there were some clear mismanagement observed. Programming Competition also went great and we were able to solve 3/5 questions in given time. And when results were announced, we were at 2nd position!

Microsoft Student Ambassadors’ Meeting

August 22, 2006 by Munir

This week started with the end of summer vacations and start of a new semester. Now it is just a year left to complete my bachelor’s degree. Woohoo! for that :-)

Well, today after taking classes, I reached to Microsoft Pakistan office to attend monthly Student Ambassadors Meeting with Microsoft ISV Manager Mr. Vaqar Khemasani. Many things were discussed in meeting including Microsoft’s future events, Imagine Cup 2007 and some announcements by Microsoft. A seminar on “Software Project Ideas and Imagine Cup 2007″ was also planned to be arranged on 31st of this month in my campus. It was really great to meet with Microsoft Pakistan officials and Student Ambassadors of other universities.

Wonderful Weather!

August 17, 2006 by Munir

I was bit sad as I didn’t enjoy well the rains in this summer before because of fever and cold. After many days I am feeling much better now and fortunately it is rain again today!

Wow, what a beautiful weather it is! Feeling great while enjoying the weather with family and friends.

I am almost soaked now and my mother is scolding me. She is worried I will catch cold again but I am sure it won’t happen again :)

I am going to top of building to enjoy it again!!

Finding Memory Leaks Using mtrace

August 5, 2006 by Munir

mtrace is a nice GNU extension which is used to diagnose dynamic memory allocation in C/C++ programs. It can also be used for finding memory leaks. I am going to demonstrate a memory leak problem in a simple C++ program. Look at the following code:


// File: m-test.cpp

#include <iostream>
#include <cstdlib>

#define SIZE 16

using namespace std;

bool isPalindrome(char *);

int main()
{
  char *str;
  while(true)
  {
    str = (char *)malloc(SIZE);
    cout << "Enter string to check Palindrome or quit to exit: ";
    cin >> str;
    if(strcmp(str, "quit") == 0)
      break;
    if(isPalindrome(str))
      cout << str << " is a Palindrome." << endl;
    else
      cout << str << " is NOT a Palindrome." << endl;
  }
  return 0;
}

bool isPalindrome(char *str)
{
  int str_len = strlen(str);
  for(int i = 0; i < str_len/2; i++)
    if(str[i] != str[str_len-i-1])
      return false;
  return true;
}

It is a simple C++ program which checks whether the input string is a palindrome or not. I compiled and ran using following commands and it exeucutes perfectly:


munir@ubuntu:~/prog/c$ g++ m-test.cpp -Wall -g -o m-test
munir@ubuntu:~/prog/c$ ./m-test
Enter string to check Palindrome or quit to exit: ababa
ababa is a Palindrome.
Enter string to check Palindrome or quit to exit: abb
abb is NOT a Palindrome.
Enter string to check Palindrome or quit to exit: quit
munir@ubuntu:~/prog/c$

But did you find any thing wrong by reading code?

There is something wrong with dynamic memory allocation by using malloc(). Note that malloc(SIZE) is called every time when the program takes input from command promt. Lets trace the memory allocation using mtrace by following easy steps:

1) Include mcheck.h header file.
2) Place mtrace() in the first line of main(). mtrace will start tracing memory from this point.
3) Place muntrace() in the end of main() to stop tracing.
4) At command promt type export MALLOC_TRACE=mtrace.log to save output in mtrace.log file.
5) Compile and run the program again.

It should execute exactly like before but this time it will create a new file mtrace.log. Now come to the fun part. Open the text from mtrace.log:


munir@ubuntu:~/prog/c$ ./m-test2
Enter string to check Palindrome or quit to exit: aba
aba is a Palindrome.
Enter string to check Palindrome or quit to exit: a
a is a Palindrome.
Enter string to check Palindrome or quit to exit: aa
aa is a Palindrome.
Enter string to check Palindrome or quit to exit: abb
abb is NOT a Palindrome.
Enter string to check Palindrome or quit to exit: anm
anm is NOT a Palindrome.
Enter string to check Palindrome or quit to exit: quit

munir@ubuntu:~/prog/c$ cat mtrace.log
= Start
@ ./m-test2:(__gxx_personality_v0+0x161)[0x80488e5] + 0x804a378 0x10
@ ./m-test2:(__gxx_personality_v0+0x161)[0x80488e5] + 0x804a390 0x10
@ ./m-test2:(__gxx_personality_v0+0x161)[0x80488e5] + 0x804a3a8 0x10
@ ./m-test2:(__gxx_personality_v0+0x161)[0x80488e5] + 0x804a3c0 0x10
@ ./m-test2:(__gxx_personality_v0+0x161)[0x80488e5] + 0x804a3d8 0x10
@ ./m-test2:(__gxx_personality_v0+0x161)[0x80488e5] + 0x804a3f0 0x10
= End
munir@ubuntu:~/prog/c$


This is not in very readable form but what I understood from this result is that my program was allocated 0x10 bytes(16 bytes in decimal) of memory 6 times because malloc() was called 6 times.

Since the content of the log file is not very clear to understand therefore I am going to use a useful Perl script to interpret the results. This script is also called mtrace and its syntax is very simple to use: mtrace program log-file


munir@ubuntu:~/prog/c$ mtrace m-test2 mtrace.log

Memory not freed:
-----------------
   Address     Size     Caller
0x0804a378     0x10  at /home/munir/prog/c/m-test2.cpp:17
0x0804a390     0x10  at /home/munir/prog/c/m-test2.cpp:17
0x0804a3a8     0x10  at /home/munir/prog/c/m-test2.cpp:17
0x0804a3c0     0x10  at /home/munir/prog/c/m-test2.cpp:17
0x0804a3d8     0x10  at /home/munir/prog/c/m-test2.cpp:17
0x0804a3f0     0x10  at /home/munir/prog/c/m-test2.cpp:17
munir@ubuntu:~/prog/c$

This indicates that there is a potential memory leak in program at line 17 which is caused by dynamic memory allocation by calling malloc() and the program does not free the allocated memory. free() must be used in order to solve the problem. Here is the final version with no such memory leak:


// File: m-test3.cpp

#include <iostream>
#include <cstdlib>

#define SIZE 16

using namespace std;

bool isPalindrome(char *);

int main()
{
  char *str;
  while(true)
  {
    str = (char *)malloc(SIZE);
    cout << "Enter string to check Palindrome or quit to exit: ";
    cin >> str;
    if(strcmp(str, "quit") == 0)
    {
      free(str);
      break;
    }
    if(isPalindrome(str))
      cout << str << " is a Palindrome." << endl;
    else
      cout << str << " is NOT a Palindrome." << endl;
    free(str);
  }
  return 0;
}

bool isPalindrome(char *str)
{
  int str_len = strlen(str);
  for(int i = 0; i < str_len/2; i++)
    if(str[i] != str[str_len-i-1])
      return false;
  return true;
}

And running the test again:


munir@ubuntu:~/prog/c$ g++ m-test3.cpp -Wall -g -o m-test3
munir@ubuntu:~/prog/c$ ./m-test3
Enter string to check Palindrome or quit to exit: a
a is a Palindrome.
Enter string to check Palindrome or quit to exit: aaa
aaa is a Palindrome.
Enter string to check Palindrome or quit to exit: abc
abc is NOT a Palindrome.
Enter string to check Palindrome or quit to exit: aBbabBa
aBbabBa is a Palindrome.
Enter string to check Palindrome or quit to exit: aBbabBA
aBbabBA is NOT a Palindrome.
Enter string to check Palindrome or quit to exit: quit
munir@ubuntu:~/prog/c$ cat mtrace.log
= Start
@ ./m-test3:(__gxx_personality_v0+0x171)[0x8048915] + 0x804a378 0x10
@ ./m-test3:(__gxx_personality_v0+0x244)[0x80489e8] - 0x804a378
@ ./m-test3:(__gxx_personality_v0+0x171)[0x8048915] + 0x804a378 0x10
@ ./m-test3:(__gxx_personality_v0+0x244)[0x80489e8] - 0x804a378
@ ./m-test3:(__gxx_personality_v0+0x171)[0x8048915] + 0x804a378 0x10
@ ./m-test3:(__gxx_personality_v0+0x244)[0x80489e8] - 0x804a378
@ ./m-test3:(__gxx_personality_v0+0x171)[0x8048915] + 0x804a378 0x10
@ ./m-test3:(__gxx_personality_v0+0x244)[0x80489e8] - 0x804a378
@ ./m-test3:(__gxx_personality_v0+0x171)[0x8048915] + 0x804a378 0x10
@ ./m-test3:(__gxx_personality_v0+0x244)[0x80489e8] - 0x804a378
@ ./m-test3:(__gxx_personality_v0+0x171)[0x8048915] + 0x804a378 0x10
@ ./m-test3:(__gxx_personality_v0+0x1bd)[0x8048961] - 0x804a378
= End
munir@ubuntu:~/prog/c$ mtrace m-test3 mtrace.log
No memory leaks.
munir@ubuntu:~/prog/c$

So we just saw how easy is to detect memory leak using mtrace utility.

Source codes: m-test.cpp m-test2.cpp m-test3.cpp
Screen shot: here

Simple TCP Port Scanner

July 26, 2006 by Munir

During the course of Computer Communication in last semester, I tried to develop some network tools in C#. Among all, I found it most interesting to develop a simple multi-threaded TCP port scanner using some built in classes of System.Net namespace. Today I modified it bit more and added some options to make it customizable. You can get its source from here and complete VS 2005 project from here.

Update: Some design improvements. Thanks to Whiz Kid for suggestions.

Different kinds of people

July 24, 2006 by Munir

There are 10 types of people in this world: people who understand binary, and people who have friends. But there are actually 11 types of people in this world: those who understand Gray, and those who don’t. No, in fact there are only 10 types of people: those who know ternary, those who don’t, and those who confuse it with binary.