Friday, June 3, 2011

Linux running on JavaScript

Suddenly came across this amazing project.. Linux running ‘on’ the web browser. It’s a virtual machine written in JavaScript, it can boot a small linux image supporting basic commands, file system, vi, and even.. a C compiler!


You can edit, compile and run the sample hello.c program using the compiler called tcc.

To compile: tcc -o hello hello.c
To run: ./hello

All that running on top of JavaScript!.. absolutely mind blowing!

By the way, only the following browsers are supported:
  • Firefox 4.x
  • Chrome 11
  • Opera 11.11
  • Internet Explorer 9

Sunday, April 24, 2011

C++: Is 'this' safe in the constructor?

I've been working hard on something very interesting during the Easter holidays. Wanted to take a quick break, listen to some good music and make a blog post. Alright, my favorite Sony earbuds are on and queued the songs. Now here goes the quick post.

Very often C++ beginners get confused about when and how 'this' pointer can be used. Most common confusion is probably:

Is it safe to use 'this' in the constructor?

Having the confusion, many amateurs avoid using 'this' pointer in constructor. Well, such confusion is harmful for the programmer, project and the company funding the project.

The short answer to the question is: yes, 'this' is very safe in the constructor. All members (or objects) derived (from parent class in case of inheritance) and defined in the class 'will be' allocated and initialized before the constructor gets executed. So you can safely access the members of the class in the constructor.

Now, that was a short answer. For a comprehensive one, we need to discuss in what order objects are created and destroyed and how to use the RAII (Resource Acquisition Is Initialization) idiom. I'll try to make this the topic of my next post.

For a brief discussion on how objects are initialized, you can read this post:

Talking about 'this' reminds me of the 'suicidal code'. Six years ago, I have seen the following use of 'this' in a very important server component:

//now commit suicide
delete this;

Needless to say that's an architectural disaster. Many of the times we see such disasters in codes of fresh grades. A good way to avoid this is to train your new team mates on idioms like RAII.

Stay tuned for my next post :).

Friday, April 15, 2011

C++ is not C

C++ compilers (g++ or visual c++ for example) can compile many C codes, but not necessarily all of them. Here is a simple but lame example:

int class, template;

Above code compiles fine with a C code but not with C++, as 'class' and 'template' are keywords in C++. But again, it's really a lame example.

A good example is the feature set of C99. Here are some codes that are valid in C but not in C++:

int vec[5] = { [1]=10, [3]=20 }; // designated initializers

typedef struct
char name[20];
int ID;
int age;

Employee emp = {.ID = 0, .age = 0};

int main()

The reason for not having the support for the C99 code in some major C++ compilers is that C++ was standardized in 98 and C99 standards came after that.

Will try to post more on this later.

C++: WorldLight

WordLight is a nice little add-in for Visual Studio 2008. When you select some text in the editor, it searches and highlights the same string in rest of the code.


If you have VisualAssist, you may not need this. Otherwise, you'll enjoy this add-in.

Windows Directory Statistics

The terabytes of hard drives in the market rarely makes us concerned about disk space utilization. However we don't get that luxury for solid state drives. I was running out of disk space with the 128 GB SSD in my work PC and needed to clean up a bit. This tool helped me to do that better.

Windows Directory Statistics

It helps you to understand disk utilization and clean up space more easily. It shows disk, file and directory sizes in a treelist as well as graphically in a treemap.

Monday, June 21, 2010

C++: Convert string to int or vice-versa

strinstream is very useful for many basic jobs with strings, for example parsing numeric data from a string, converting a string to int/float, converting int/float to string, etc.

Here goes examples of how to use stringstream to convert to and from strings.

#include <iostream>
#include <sstream>

using namespace std;

int main()
stringstream ss;
string s;
int n;

//to convert int to string
n = 1024;

ss << n; //write the int to stringstream
ss >> s; //read back the value as a string
cout << "string value: " << s << endl;

//to convert int to string
s = "2048";

ss << s; //write the string to stringstream
ss >> n; //read back the value as an int
cout << "int value: " << n << endl;


Have fun!

C++: Failed Conditions

At ReliSource, we used to have a mailing list called CPP in which we submitted C/C++ related facts/questions (and solutions). I'll share some of those facts/questions in my blog.

Here goes a quick C/C++ problem that I'm sure you will find interesting.

// x is a numeric variable (of built in/intrinsic data type).

if (x > 10)
cout << "x is > 10\n";
else if (x <= 10)
cout << "x is <= 10\n";

For some reason, the program didn't output anything, none of the if or else-if was executed. What can be the reason?

You shall find the answer in the comments after a week of this post (if someone already doesn't answer it).

Thursday, February 11, 2010

An excellent article on interviewing

Following is an excellent article on interviewing:

A colleague of mine referred to this article while we were discussing how to improve our interview process. It's a long one but quite worth reading. I felt like these are the words of my mind.

Following are some excerpts for those who are feeling lazy and/or busy :).

===Excerpts start===

You’re going to see three types of people in your interviews. At one end of the scale, there are the unwashed masses, lacking even the most basic skills for this job. They are easy to ferret out and eliminate, often just by asking two or three quick questions. At the other extreme you’ve got your brilliant superstars who write lisp compilers for fun, in a weekend, in Assembler for the Nintendo DS. And in the middle, you have a large number of “maybes” who seem like they might just be able to contribute something. The trick is telling the difference between the superstars and the maybes, because the secret is that you don’t want to hire any of the maybes. Ever.


How do you detect smart in an interview? The first good sign is that you don’t have to explain things over and over again. The conversation just flows. Often, the candidate says something that shows real insight, or brains, or mental acuity. So an important part of the interview is creating a situation where someone can show you how smart they are.


The worst kind of interviewer is the blowhard. That’s the kind who blabs the whole time and barely leaves the candidate time to say, “yes, that’s so true, I couldn’t agree with you more.” Blowhards hire everyone; they think that the candidate must be smart because “he thinks so much like me!”

The second worst kind of interviewer is the Quiz Show Interviewer. This is the kind of person who thinks that smart means “knows a lot of facts.” They just ask a bunch of trivia questions about programming and give points for correct answers. Just for fun, here is the worst interview question on Earth: “What’s the difference between varchar and varchar2 in Oracle 8i?” This is a terrible question. There is no possible, imaginable correlation between people that know that particular piece of trivia and people that you want to hire. Who cares what the difference is? You can find out online in about fifteen seconds!


What should you look for during the open ended questions?
One: Look for passion. Smart people are passionate about the projects they work on. They get very excited talking about the subject. They talk quickly, and get animated.


These softball questions seem too easy, so when I first started asking them, I had to admit that I really expected everyone to sail right through them. What I discovered was that everybody solved the problem, but there was a lot of variation in how long it took them to solve.


Serge Lang, a math professor at Yale, used to give his Calculus students a fairly simple algebra problem on the first day of classes, one which almost everyone could solve, but some of them solved it as quickly as they could write while others took a while, and Professor Lang claimed that all of the students who solved the problem as quickly as they could write would get an A in the Calculus course, and all the others wouldn’t. The speed with which they solved a simple algebra problem was as good a predictor of the final grade in Calculus as a whole semester of homework, tests, midterms, and a final.

You see, if you can’t whiz through the easy stuff at 100 m.p.h., you’re never gonna get the advanced stuff.


15 years of experience interviewing programmers has convinced me that the best programmers all have an easy aptitude for dealing with multiple levels of abstraction simultaneously. In programming, that means specifically that they have no problem with recursion (which involves holding in your head multiple levels of the call stack at the same time) or complex pointer-based algorithms (where the address of an object is sort of like an abstract representation of the object itself).

I’ve come to realize that understanding pointers in C is not a skill, it’s an aptitude. In first year computer science classes, there are always about 200 kids at the beginning of the semester, all of whom wrote complex adventure games in BASIC for their PCs when they were 4 years old. They are having a good ol’ time learning C or Pascal in college, until one day they professor introduces pointers, and suddenly, they don’t get it. They just don’t understand anything any more.

For some reason most people seem to be born without the part of the brain that understands pointers. Pointers require a complex form of doubly-indirected thinking that some people just can’t do, and it’s pretty crucial to good programming.


A lot of the “script jocks” who started programming by copying JavaScript snippets into their web pages and went on to learn Perl never learned about pointers, and they can never quite produce code of the quality you need.


Sadly, despite the fact that I think that all good programmers should be able to handle recursion and pointers, and that this is an excellent way to tell if someone is a good programmer, the truth is that these days, programming languages have almost completely made that specific art unnecessary.


A lot of programmers that you might interview these days are apt to consider recursion, pointers, and even data structures to be a silly implementation detail which has been abstracted away by today’s many happy programming languages. “When was the last time you had to write a sorting algorithm?” they snicker.

Still, I don’t really care. I want my ER doctor to understand anatomy, even if all she has to do is put the computerized defibrillator nodes on my chest and push the big red button, and I want programmers to know programming down to the CPU level, even if Ruby on Rails does read your mind and build a complete Web 2.0 social collaborative networking site for you with three clicks of the mouse.


Monday, September 7, 2009

Maemo 5 - Linux at the core of next generation mobile OS

It's been long since I felt excited about any new mobile OS. Maemo 5 seems to be a very promising mobile OS for the next generation of mobile devices.

Multiple desktops, multitasking, Mozilla based browser, desktop widgets, cool UI, speed all seems to be best of what other OS has to offer. And guess what, it runs on Linux. The Symbian OS 9.4, Windows Mobile 6.5 or iPhone OS 3.. all seem to be moving ahead somewhat slowly. Comparing to them, Maemo 5 seems to be a leap ahead. It seems to be the next best thing after iPhone OS 1 came out thousands of years ago :).

Nokia will release N900 in a few months, the company's first phone running on Maemo 5. N900 itself has good spec with 600 MHz ARM processor, 256 MB RAM, 32 GB ROM, high-end cam, etc.

You can check more about Maemo at: and more about N900 at

Wednesday, February 18, 2009

C++: Pointer to members

Pointer to member operators are not an everyday operators that we use but those can be useful if used in the correct way. Pointer to member operator ::* is used to declare a pointer that points to a member variable, pointer or function of a class. The operators .* and ->* are used to access it.

Following is an example of how we can use Pointer to member function (also called member function pointer).

  1 #include <iostream>
2 using namespace std;
4 class A{
5 public:
6 A(){
7 }
8 int Func(int a){
9 return a * 2;
10 }
12 };
14 int main(){
15 int (A::*fp)(int) = &A::Func;
17 A a;
18 cout << (a.*fp)(10) << endl;
20 return 0;
21 }

Following is another (almost random) example of how we can write generic classes to wrap pointer to member function to use with multiple classes.

  1 #include <iostream>
2 using namespace std;
4 class A{
5 public:
6 int Func1(int a){
7 return a * 2;
8 }
10 };
12 class B{
13 public:
14 int Func2(int a){
15 return a / 2;
16 }
18 };
20 template <class T>
21 class MemFnPtr
22 {
23 public:
24 MemFnPtr(int (T::*fp)(int), T &t) : m_fp(fp), m_t(t){
25 }
26 int Fn(int n)
27 {
28 return ((m_t).*(m_fp))(n);
29 }
31 //member vars
32 int (T::*m_fp)(int);
33 T &m_t;
34 };
36 int main(){
37 A a_obj;
39 MemFnPtr<A> fp_a(&A::Func1, a_obj);
40 cout << fp_a.Fn(10) << endl;
42 B b_obj;
44 MemFnPtr<B> fp_b(&B::Func2, b_obj);
45 cout << fp_b.Fn(10) << endl;
47 return 0;
48 }