Archive for the ‘Programming’ Category

Unix Timestamp in .NET

October 16, 2008

In one of my current projects, I’m required to calculate Unix Timestamps in my .NET classes. Unix Timestamp is defined as number of second elapsed since January 1, 1970 00:00:00.

.NET also provides a very useful class System.DateTime for Date and Time calculation. Unlike Unix standard, DateTime type in .NET represents date and time values from 12:00:00 midnight, January 1, 0001 A.D. through 11:59:59 P.M., December 31, 9999 A.D. measured in 100-nanoseconds units called Ticks.

Using above facts, here is the simple code for DateTime conversion between these two standards:


long ToUnixTimestamp(System.DateTime dt)
{
    DateTime unixRef = new DateTime(1970, 1, 1, 0, 0, 0);
    return (dt.Ticks - unixRef.Ticks) / 10000000;
}
DateTime FromUnixTimestamp(long timestamp)
{
    DateTime unixRef = new DateTime(1970, 1, 1, 0, 0, 0);
    return unixRef.AddSeconds(timestamp);
}

Imagine Cup Launch and Workshop on Web Development in Campus

January 14, 2007

Wow it was quite an amazing day!

FAST.net user group and IEEE-NUCES organized Imagine Cup 2007 Launch and a 3 hour workshop on Web development using ASP.net on Friday, 12th January. Due to vacations many students couldn’t participate but those who attended this event appreciated it a lot. I and Ali Raza Shaikh were the speakers:

From Imagine Cup L…
From Imagine Cup L…

During the imagine Cup session, we showed promo videos and interviews of past winners to motivate students. We also distributed free CDs of Visual Studio and SQL Server 2005 Express Edition to help them in getting started.

From Imagine Cup L…

Workshop on ASP.net also went great and I believe student learned many new things. Since most of the students were from junior and sophomore batches, we didn’t go in much technical depth.

From Imagine Cup L…
From Imagine Cup L…

The most fun part of the event was Q/A session where we asked questions to student for Goodies like ASP.net WebMatrix Book, MSDN Magazine, Development Resource CDs. Thanks to Microsoft Pakistan and INETA for sending those goodies.

From Imagine Cup L…

I’d also like to thank all participants for making this event successful and I hope we’d be able to continue this series of workshops on web development with the support of Microsoft and INETA.

Microsoft Tech-Ed Developer Conference 2006

October 8, 2006

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

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!

Finding Memory Leaks Using mtrace

August 5, 2006

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

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.

w00t! I am green now

July 22, 2006

I last participated in TopCoder Single Round Match about two months ago and during that period I had completely forgotten it because of semester exams and other activities. But after this long gap today I participated again. Not only I played better than my expectation but also my algorithm rating has increased to 950 and I am green coder now!

You can see my room statistics here.