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!

URL: http://bellard.org/jslinux/

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:
http://kaisar-haque.blogspot.com/2008/06/c-avoid-using-assignments-in.html

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;

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.

URL: http://code.google.com/p/wordlight/

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
URL: http://sourceforge.net/projects/windirstat/

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.