Archive for November, 2010

c++ arrays memo

November 30th, 2010

1. arrays initialization

under GCC, when you create an array statically, it’s not initialized. while you create it dynamically, it’s initialized.
here is the test code:

int main () {

	int N(1000000);

	// statically created array, not initialized
	int a[N];
	for(int i = 0; i < N; ++i) {
		if(a[i] != 0) {
			cout << "a[N] is not all zero!!!" << endl;
			break;
		}
	}

	// dynamically created array, initialized
	int *b;
	b = new int[N];
	for(int i = 0; i < N; ++i) {
		if(b[i] != 0) {
			cout << "b[N] is not all zero!!!" << endl;
			break;
		}
	}
	// dynamically created array, initialized	
	int *c;
	c = new int[N]();
	for(int i = 0; i < N; ++i) {
		if(c[i] != 0) {
			cout << "c[N] is not all zero!!!" << endl;
			break;
		}
	}
}

2. return array from function

Arrays of other types are typically returned via reference:

void retArray( (int*)& p, unsigned& size )
{
	size = 4;
	p = new int[ size ];
	for (int n = 1; n < 4; n++)
		p[ n ] = n;
}

kill a process by name on Linux

November 23rd, 2010

Way 1: find the pid of the process and then kill it by pid.

Step 1: How to find pid:
$> ps aux | grep amsn
output:

weiping 7208 0.4 2.2 260684 86808 ? Sl 09:22 0:15 wish8.5 /usr/bin/amsn weiping 7804 0.0 0.0 8952 876 pts/10 S+ 10:15 0:00 grep amsn

or
$> pidof wish8.5
output:

7208

Step 2: Kill the process
$> kill 7208

Way 2: use killall

killall -9 wish8.5

Way 3: use pkill

pkill wish8.5

Way 4: use ps + kill

export sspid=`ps aux | grep wish8.5| awk 'NR==1{print $2}' | cut -d' ' -f1`;kill $sspid

bash commands

November 12th, 2010

1. for loop

for (( EXP1; EXP2; EXP3 ))
do
	command1
	command2
	command3
done

eg:

#!/bin/bash
for (( i=0; i<5; i++ ))
do
	echo "Hello $i..."
done

2. if / else / fi statements

basic grammar (note the spaces between the [ ]):

if [ EXP ]; then
	statements...
else
	statements 2...
fi

eg:

step=5
alpha=$(echo "scale=1; $step/10" | bc)
if [ $(echo "$alpha<1" | bc) -eq 1 ]; then
	alpha=0$alpha
fi

2.1 Arithmetic Comparisons

-lt <
-gt >
-le <=
-ge >=
-eq ==
-ne !=

2.2 String Comparisons

= equal
!= not equal
< less then
> greater then
-n s1 string s1 is not empty
-z s1 string s1 is empty

3. real number calculation

(echo "scale=2; EXP" | bc)

where scale=2 sets the number of the digits after decimal point.
eg1:

a=1;b=2;
c=$(echo "scale=2; $a/$b" | bc)
echo $c

output:

.50

eg2:

exp="1223/234";
d=$(echo "scale=2; ${exp}" | bc)
echo $d

output:

5.22

reference: http://linuxconfig.org/Bash_scripting_Tutorial

4. functions

basic grammar:

function div() {
    STATEMENTS
    return XXX
}>
NOTE: A bash function can only return a number. There is no way to return directly a string. So we need to use $() to capture the result outputted in the function body.
eg:
# ----------------------------------------------
# return $1/$2 with 1 digit after decimal point.
# ----------------------------------------------
function div() {
   res=$(echo "scale=1; $1/$2" | bc)
   if [ $(echo "$res<1" | bc) -eq 1 ]; then
      res=0$res
   fi
   echo $res
}
# use the function
step=5
alpha=$(div $step 10)