Thursday, February 1, 2007

A simple way to download videos from sites like YouTube

There's a really simple way to download videos from sites like YouTube. This's what you need to do:
1. Copy the URL that appear in the address bar and paste it here.
2. Rename the file to have .flv extension.
3. View it in a flash video viewer[Example: FLV Player 1.3]!

All time favorites..

All time favorites at Piled Higher and Deeper .. :)

Motivation graph
Work output graph

Function Pointers and Functors in C++ [in 5 minutes]

Function pointers are very useful technique to provide late binding, to prevent switching and to implement callbacks in C/C++. Here are some example cases of C/C++ function pointers and C++ functors which can be used to eliminate switches inside your code. (Remember the design principle? The code should be closed to modification but open to extension!)

Here's the C style of doing it:


#include <stdio.h>

/* calculate2, calcualte2, etc are functions
with the same prototype */
int calculate1(int val)
{
return val*val;
}

int calculate2(int val)
{
return val+val;
}

/* passing a function pointer */
int useCalculate(int (*fnPtr)(int), int someVal)
{
return fnPtr(someVal);
}

/* using the function which takes a function pointer */
int main()
{
printf("Using calculate1 = %d\n",
useCalculate(calculate1, 10));
printf("Using calculate2 = %d\n",
useCalculate(calculate2, 10));
return 0;
}


Here's the C++ style of doing it (with method pointers):

#include <iostream>

using namespace std;

//calcualte1, calculate2 are methods with the same prototype
class Util
{
public:
int calculate1(int val)
{
return val*val;
}
int calculate2(int val)
{
return val+val;
}
};

//the above prototype is taken as a formal argument here
class UseUtil
{
Util utilObj;

public:
int useCalculate(int (Util::*methodPtr)(int), int someVal)
{
return (utilObj.*methodPtr)(someVal);
}

};

//using the method with method pointer argument
int main()
{
UseUtil useUtilObj;
cout << "Using Calculate1 = " <<
useUtilObj.useCalculate(&Util::calculate1, 10) << "\n";
cout << "Using Calculate2 = " <<
useUtilObj.useCalculate(&Util::calculate2, 10) << "\n";
return 0;
}


Here's the C++ style of doing it again (but with functors!):

#include <iostream>
using namespace std;

//it takes a "stateless functor class"
template
class UseUtil
{
public:
int useCalculate(UtilFunctor calculate, int someVal)
{
return calculate.operator()(someVal);
}

};

//out of line way
//int UseUtil::useCalculate(
// UtilFunctor calculate, int someVal)
//{
// return calculate(someVal);
//}

//first functor class
class Cal1
{
public:
int operator()(int val)
{
return val*val;
};
};

//second functor class
class Cal2
{
public:
int operator()(int val)
{
return val+val;
}
};

int main()
{
UseUtil useUtilObj1;
Cal1 calObj1;
cout << "Using Cal1 = " <<
useUtilObj1.useCalculate(calObj1, 10) << "\n";
UseUtil useUtilObj2;
Cal2 calObj2;
cout << "Using Cal2 = " <<
useUtilObj2.useCalculate(calObj2, 10) << "\n";
return 0;
}

Sunday, January 28, 2007

Passing objects into Methods in Java

Having worked with C/C++ extensively for couple of year, now I am refreshing my memory on Java..It's kinda difficult to do away with C/C++ practices :) Thought the following post might be of interest to you.

Is parameter passing in Java by reference or by value?
Bottomline: Everything in java is passed by value. But objects are NEVER passed to the method!

myth: objects are passed by reference, primitives are passed by value. (This is wrong)

Pass by reference means, you are working with the actual pointer that points to the object. For example, this is how C++ works.


When java passes an object to a method, it first makes a copy of a reference to the object, not a copy of the object itself.

The following example illustrates the point.

public class PassByValueTest {

class Point {
int x;
int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}

public void setPoint(int x, int y) {
this.x = x;
this.y = y;
}

public String toString() {
return "Point[x="+x+",y="+y+"]";
}
}

public void method1(Point pt){
pt.setPoint(5, 5);
}

public void method2(Point pt){
pt = new Point(10,10);
}

public Point createPoint(){
return new Point(1,1);
}


public static void main(String [] args){
PassByValueTest test = new PassByValueTest();
Point p = test.createPoint();
System.out.println("Before calling method1: "+ p.toString());
strTest.method1(p);
System.out.println("After calling method1: "+ p.toString());
strTest.method2(p);
System.out.println("After calling method2: "+ p.toString());
}

Output:
Before calling method1: Point[x=1,y=1]
After calling method1: Point[x=5,y=5]
After calling method2: Point[x=5,y=5]

Notice that values inside the Point object get changed after calling method1, but not after method2. That's becasue, a copy of a reference to Point object p is passed, not the actual refernce itself. This is the key difference from languages like C++.

ref1(address) ---> p (object) <--- copyref1(address)

copyref1 is passed into method1. We modify the object pointed to by copyref1 which is the actual object. Therefore the mofications remain even after calling method1.

copyref1 is passed into method2. When our code gets out of method2, it does not affect the object pointed to by ref1. The new object created inside method2 simply garbage collected by the JVM.

Futher reading:
http://www.yoda.arachsys.com/java/passing.html
http://www-128.ibm.com/developerworks/library/j-praxis/pr1.html
http://javadude.com/articles/passbyvalue.htm

Monday, January 22, 2007

Beware of "Storm Worm"

Storm Worm is a trojan horse attack baiting people with weather information. It spreads through emails. The email carries the subject "230 dead as storm batters Europe".

People who open the attachment becomes part of a botnet, which attackers can exploit later without the knowledge of the user. It's kinda social engineering in that it tries to fool people by giving timely alert about current deadly weather conditions prevailing in most parts of the world. The attachment is an executable file which promises to give more details about the weather conditions. Opening the attachement creates a backdoor which can be later exploited later to steal data or use post malicious spams.

Sunday, January 21, 2007

Introduction to C++ Programming [Part 3]

Previous Related Posts: Part1 Part2

C++ Data Types

There are two groups of built-in data-types; fundamental types and derived types. The fundamental types represent integers and floating-point numbers. The derived types include arrays, strings, pointers and structures. We’ll first look at fundamental types. The following table gives you the data types available, their memory usage and the range of values they can take.


Data Type

Size (in bytes)

Range of values

unsigned short int

2

0 to 65,535

short int

2

-32,768 to 32,767

unsigned long int

4

0 to 4,294,967,295

long int

4

-2,147,483,648 to 2,147,483,647

int (16 bit)

2

-32,768 to 32,767

int (32 bit)

4

-2,147,483,648 to 2,147,483,647

unsigned int (16 bit)

2

0 to 65,535

unsigned int (32 bit)

2

0 to 4,294,967,295

char

1

256 character values

float

4

1.2e-38 to 3.4e38

Double

8

2.2e-308 to 1.8e308

bool

1

true or false



It should be noted that sizes of the data types vary with the platform. C++ standard does not specify exact sizes for data types, as no one choice is suitable for all computer designs. C++ offers a flexible standard with some guaranteed minimum sizes;

· A short integer is at least 16 bits

· An integer is at least as big as short

· A long integer is at least 32 bits and at least as big as int.

Unsigned types don’t hold negative values. To represent floating-point numbers you can either use decimal representation (e.g.: 234.45) or use E notation (e.g.: 2,3445e+2).

A word about the type bool is in order. This type was recently introduced to C++. Earlier C++ used to interpret nonzero values as true and zero values as false (this is still valid). Now you can use the type bool to represent true and false, and the predefined literals true and false represent those values.


Variables

To store an item of information in a computer, the program needs to keep track of three fundamental properties.

· Where the information is stored

· What value is kept there

· What kind of information is stored

We declare variables to keep track of these properties.

Syntax:

Data_type variable_name; //declare a variable

Variable_name = value; //assign a value

Or

Data_type variable_name = value; //declare and assign a value

e.g.: int iAge;

unsigned int uiCounter;

You can create more than one variable of the same type in one statement.

e.g.: long lSize, lWeight;

int iAge = 30, iID, iCityCode = 501;

The type used in the declaration describes the kind of information, and the variable name represents the value symbolically. The program allocates large enough memory space to hold the data. A variable can hold different values of the same data type during the execution of the program.

Naming Rules:

· The only characters you can use in names are alphabetic characters, digits and the underscore character.

· The first character in a name cannot be a digit.

· Variable names are case sensitive.

· You can’t use a C++ keyword for a name. (e.g.: void, return , main)

· C++ places no limits on the length of a name, and all characters in a name are significant.

C++ is a strongly typed language, meaning that you must declare any variable before it is used in the program.

e.g.:

#include <iostream>

using namespace std;

int main()

{

int iIntValue = INT_MAX; //initialize iIntvalue to max int value

cout << "int takes " <<"\n";

cout << "int takes " <<"\n";

cout << "The max int value is "<<"\n";

return 0;

}

INT_MAX is a symbol defined in limits.h file and represents the largest possible value that an int can take. sizeof() function (defined in the compiler) takes either the data type or variable name and returns the number of bytes it occupies in the memory.

The output would be something similar to the following. (Exact values might vary with the system you are using)

int takes 4 bytes

int takes 4 bytes

The max int value is 2147483647

C++ allows you to create aliases for data types. We use the typedef keyword to do so.

Syntax: typedef type type_name;

e.g.: Creating an alias for the type unsigned short int

typedef unsigned short int USHORT;

Now you can use USHORT instead of unsigned short int.

e.g.: USHORT usMyVariable;

Constants

After you initialize a constant, its value is set; the compiler does not let you subsequently change the value. It is a good programming practice to define constants rather than using numeric or character values. When you want to change the value, you only need to change the constant declaration.

Syntax:

const type name = value;

e.g.: const int DAYS = 7;

The const qualifier is used to indicate that the item is a constant. You can assign a value to a constant only when you declare it.

You can use #define statement (e.g.: #define DAYS 7) to create symbolic constants. But it is better to use this only when it is absolutely necessary. const lets you specify the type explicitly, but #define doesn’t. Further, you can use C++’s scoping rules (which we’ll look at under functions) to limit the definition to particular functions or files.

The C++ enum facility provides an alternative means to const for creating symbolic constants, which we’ll be looking at under derived types.

In the next lesson, we'll be starting from operators and control structures.

Implications of shooting down satellites

As you have probably heard in news, China recently shot down one of their own satellites from ground as part of missile testing.


(Courtesy: Time magazine)

Implications:
In the event of a major war, first thing the china would probably do to shoot down all the lower orbit spy satellites.
At the moment, it seems that China is demonstrating military capabilities mainly at United States.
Possible damages that can be caused by the space debris left by the shooting.
New international laws to prevent activities in space which is harmful to many nations.

Here's the article on Time magazine for more information.