Pointer

2007年8月23日 14点01分 没有评论

The duties of the pointer were to point out, by calling their names, those in the congregation who should take note of some point made in the sermon.

What is a pointer?

A pointer is a variable whose value is used to point to another variable. From the definition, you know two things: first, that a pointer is a variable, so you can assign different values to a pointer and second, that the values contained by a pointer must be an address that indicates the location of another variable in the memory. That’s why a pointer is also called an address variable.

Address(Left Value) versus Contents(Right Value)
Each memory location must have a unique address so that the computer can read from or write to the memory location without any confusion. This is similar to the concept that each house in a city must have a unique address. Right value, can be thought as a letter deliveved to the mailbox.

Note, when your C program is being compiled, and a value is being assigned to a variable, the C compiler has to check the left value of the variable. If the compiler cannot find the left value, if will issue an error message saying that the variable is undefined in your program. That’s why in C, you have to declare a variable before you can use it.

The Address-of operator: &
Return the address(that is, the left value) of a variable.
eg: long int x,y;
y = &x;
assignes the address of the x to the address variable y.

The format specifier %p is used in the printf() funtion for displaying the address.(Hexadecimal Format)

Declare Pointers
A pointer is a variable, which means that a pointer has a left value and right value as well. However, both the left and right values are addresses. The left value of a pointer is used to refer to the pointer itself, whereas the right value of a pointer, which is the content of the pointer, is the address of another variable.

The general form of a pointer declaration:
data-type *pointer-name
data-type specifies the type of data to which the pointer points.
pointer-name is the name of the pointer variable.
asterisk indicates the variable is a pointer, is called dereference operator(indirection operator).

Null pointer, a pointer is said to be a null pointer when its right value is 0. Remember, a null pointer can never point to valid data. Simply assign 0 to the pointer value. eg: ptr_c = 0

Pointing to the same thing: a memory location can be pointed by more than one pointer.
eg: ptr_c1 = &c; ptr_c2 = &c;
Point to the same location in the memory.

Pointer and Guidepost:
Memory — City
Pointer — Guidepost
Pointer variable Left Value — The position of Guidepost in the city
Pointer variable Right Value — The text on the Guidepost, which is somebody’s home address
Null Pointer: Set Pointer variable Right Value as 0, means no content on the Guidepost, as empty.
Multiple Pointers: Many Guideposts are mapping to the same home address.
*Pointer — People who lives in the house that is indicated on the Guidepost.
data-type *Pointer — What’s kind of people?

分类: 科技 标签:

Control Flow and Char type

2007年8月23日 13点15分 没有评论

Condition branching(or jumping), loop category are both used for controlling flow.

if syntax:
if(expression){
statement1;
statement2;
}

else if(expression){
statement1;
statement2;
}
else{
statement1;
statement2;
}

Here expression is the condition criterion. If the expression is logical TRUE(nonzero), the statements inside the braces, are executed. If the expression is logical FALSE(zero), then the statements are skipped. Note, that the braces form a block of statements that is under the control of the if statement. If there is only one statement inside the block, the braces can be ignored. The parentness, however, must always be used to closed the condition expression. For instance:
if(x>0)
printf(“x>0n”);

The general form of the switch statement is:
switch(expression){
case expression1:
statement1;
break; //break is optional
case expression2:
statement2;
break;
default:
statement-default;
break;
}

When there’s no break, this is important feature of the switch statement. The computer continutes to execute the statements following the selected case until the end of the switch statement. If the return value of expression is equal to the constant expression expression1, the statement statement1 is executed. If however, the value of expression is not equal to any value of the constante expressions labeled by the case word, the statement-default key word is executed.

Add a break statement at the end of the statement list following every case label, if you want to exit the switch constant after the statements within a selected cases are executed.

The continute statement: Instead of breaking loop, there’re timeswhen you want to stay in a loop but skip over some statements within the loop. The continute statement causes execution to jump to the top of the loop immediately. Be aware to use this statement, as well as the break statement, may make your program hard to debug.

Goto is not recommended because it may cause your program to be unreliable and hard to debug.

The char Data Type:
An object of the char data type represents a single character of the character set used by your computer. But a computer can only store numeric code. Therebefor, characters such as A,a,B,b and so on all have a unique numeric code that is used by computers to represent the characters. Usually, a character takes 8 bits to store its numeric code.

For many computers, the ASCII code are the standard codes to represent a character set. ASCII, stands for American Standard Code for Information Interchange). The original ASCII character has set only 128 characters because it was the lower 7 bits that can be represent 2**7(128) characters. On IBM-compatible PCs, the character set is extended to contain a total of 2**8=256 characters.

分类: 科技 标签:

Review: C program

2007年8月21日 21点41分 没有评论

Manipulating Bits:
Computer data and files are made of bits(or bytes).

There’re six bit-manipulation operators in C:
&: The bitwise AND operation
|: The bitwise OR operation
^: The bitwise exclusive OR(XOR)
~: The bitwise complement operator
>>: right-shift operator
<<: left-shift operator Decimal number converts to Hexadecimal or Binary. Each digit in a hex number consists of fours bits. A bit represents a digit in a binary number. As we know, the binary is a 2-based numbering system. Each digit in a binary number is called a bit, and can be 1 or 0. If the position of a bit in a binary number is n, then the bit can have a value of 2 to the power of n. The position of a bit in a binary number is counted from the right of the binary number. The most-right bit is at the position of zero. For example: (Binary) 1000 -> 1*2**3+0*2**2+0*2**1+0*2**0 -> 8 (Decimal)
(Decimal) 10 -> 1*2**3+0*2**2+1*2**1+0*2**0 -> 1010 (Binary)

&: Compare each bit of x to the corresponding bit in y. If both bits are 1, 1 is placed at the same position of the bit in the result. If one of the bits, or two of them, is 0, 0 is placed in the result.
eg: 01 & 11 returns 01

|: However, places 1 in the result if either operand is 1.
eg: 01 | 11 returns 11

^: Place 1 in the result if either operand, but not both.
eg: 01 ^ 11 returns 10

~: Takes just one operand. This operator reverses each bit in the operand.
eg: ~01 returns 10

>>: Shifts the bits of and operand to the right.

<<: Operand shifts the bits to the left. Note that the unsigned integer format specifier with a minimum field width of 6, %6u, and the uppercase Hex format specifier with the minimum width of 4, %04X, and used in the printf() function. The unsigned integer data type(that is, the non-negative integer data type) is chosen so that the complementary value of an integer can be shown and understood easily. ?: is called condition operator x?y:z, Here x, y and z are three expressions. X contains the test condition. Y and Z present the final value of the expression, if X returns nonzero(TRUE), y is chosen; if X returns zero(FALSE), Z is chosen. ~(1< 1111 1100
3. Finally, add one to the complemented binary.
eg. 1111 1100 + 0000 0001 —-> 1111 1101

As shown above, -3 is represented as “1111 1101” in binary format. There’s another method to find its binary format(Also contains three steps):
1. Set sign bit at leftmost position, 1 represent negative, 0 represent integer:
eg. -3 —-> 1000 0011
2. Reverse all the bits except of sign bit.
eg. 1000 0011 —-> 1111 1100
3. Add 1 to the above binary format.
eg. 1111 1100 + 0000 0001 —-> 1111 1101

Compare with above two method, their results are same. (-3 has binary format as 1111 1101).

Note the follow cases:

Case One. 4 – 3 = 1
4 —-> 0000 0100
-3 —-> 1111 1101
——————–
+ (1)0000 0001 Here, the blue marked “1” is ignored.
Note, integer(indicates number > 0) has three same format(orginal,reverse,complement codes). See the result “0000 0001”,its left-most bit is set as 0, which means it is an integer(>0), and the corresponding decimal number is 1.
More attention, the sign bits of both 4 and (-3) must be took part in adding calculation.

Case Two. 1 – 2 = -1
-2 original code: 1000 0010
reverse code: 1111 1101
complement code: 1111 1101 + 0000 0001 —> 1111 1110
Hence, 1 —> 0000 0001
-2 1111 1110
———————-
+ 1111 1111 (Complement code)
Convert negative number 1111 1111 from its complement code to original code:
Complement Code: 1111 1111
Reverse Code: 1111 1111 – 0000 0001 = 1111 1110
Original Code: 1111 1110 —> 1000 0001 —> Decimal 10
Here, when convert negative number from reverse code to its original code, the sign bit must NOT be changed.

Case Three: -2 – 2 = -4
-2 Complement Code: 1111 1110
-2 1111 1110
—————————–
+ Result Comple. Code: (1)1111 1100

Here blue marked 1 is ignored.

Convert the result to decimal:
Complement Code: 1111 1100
Reverse Code: 1111 1100 – 0000 0001 = 1111 1101
Original Code: 1111 1101 —> 1000 0010 —> -4 Decimal

The ANSI standard allows you to indicate that a constant is of type unsigned by suffixing u or U to the constant.

Change Data Sizes:
Sometimes, you want to reduce the memory taken by variables, or you need to increase the storage space of certain data types. C language gives you the flexiblity to modify size of data types.

The short modifier, a data type can be modified to take less memory by using the short modifier. For instance, you can apply the short modifier to an integer variable that is 32 bits long, which migh reduce the memory taken by the variable to as litter as 16 bits. By default, a short int data type is signed number:
eg: printf(“Hex of -12345 is 0x%X”, -12345); result is 0xFFFF CFCT
printf(“Hex of -12345 is 0x%hX”, -12345); result is 0xCFCT

Long modifier: more memory to keep values from a wide range.

You can add h into the integer format specifiet like this: %hd, %hu. Long is represented as “%L” or “%l”.

Have to include the header file math.h before you can use any math functions defined in the header file.

Pow syntax:
# include double pow(double x, double y);
x is raised to the power of y.

Sqrt syntax:
# include double sqrt(double x);
non-negative sqart root of x in double data type. The function returns error if x is negative.

Pow(x, 0.5) equals to Sqrt(x).

Note, all floating point calculations including both the float and double data type, are done in double-precision arithmetic. That is, a float data variable must be converted to a double in order to carry on the calculation. After the calculation, the double has to be converted to the float variable. Therefor a float calculation may take more time. THe main reason the C supports the float type is to save memory spaces, because the double data type takes twice as much memory space for storage as the float data type does.

分类: 科技 标签:

Use ScribeFire(Firefox addon) to publish MSN blog

2007年8月21日 21点19分 没有评论

In the past three years, I had to edit my MSN space by IE in Windows, because firefox does not support MSN blog well(Sometimes format messes). Today I find a powerful tool to resolve this problem. Goes to the following link: “https://addons.mozilla.org/en-US/firefox/addon/1730”, and install ScribeFire for my firefox 1.5.07. After online installation, restart filefox, and there will a yellow iron on the below-right. Yes, it is ScribeFire.

Now I can write blog and publish on firefox. However, it seemes that I can not configure username/password by ScribeFire “Launch Account Wizard”. I have to edit in Scribe and paste the texts to blog window.

分类: 科技 标签:

Review: C program

2007年8月20日 21点29分 没有评论

The getc() function reads the next character from a file stream, and returns the character as an integer.

getchar == getc(stdin)
syntax: int getchar(void)

getchar() has its default file stream-stdin.
putc writes a character to the specified file stream.
syntax: int putc(int c, File * stream)
Here int c indicates that the output is a character saved in an integer variable C.
File stream specifies a file stream.

putchar syntax: int putchar(int c)

printf syntax:
int printf(const char* formating_string,…),it returns a negative value if an error occurs.
%d,%c are format specifiers.Please remember that you should use exactly the same number of expressions as the number of format specifies within the format string.

Hexadecimal, base 16 numbering system. A Hexadecimal number can be represented by four bits. The Hexadecimal number 0 through 9 map to decimal 0~9. upper case A~f means 10~15. Minimum Field Width, integer between the percent sign(%) and the letter in format specifies.

When the minimum field width is shorter than the width of the output, the latter is taken, and the output is still printed in full.

Align output: You can changed this and force outpyt to be left. To do so, you need to prefix the minimum field specifier with the minus sign(-).

Assignment Operator =
Relational Operator == (equal-to operator)
+=: do the addtion and the assignment later.
-: unary minus operator

++x: x = x + 1
pre-increment operator: the operator adds 1 to x first and then get the values of x.
–x: pre-decrement operator.

x++: x = x + 1
post-increment. For exampel, in the y = x++; statement, y is assigned the original value of x, not the one after x is increase by 1. In other words, the post-increment operator makes a copy of the original value of x and stores the copyu in a temporary location. Then x is increased by 1. However, instead of the modified value of x, the copy of the unmodified value of x is returned and assigned to y.

Relational Operations:
== Equal to
!= Not equal to
> Greater than
< Less than >= Greater than and Equal
<= Less than and Equal Important, all relational expression product a result of either 0 or 1. If a relation expression returns 1 if the specified relationship holds. Otherwise, 0 is returned. Be careful when you compare two values for equality. Because of the truncation, or rounding up, some relational expressions, which are algebraically true, may return 0 instead of 1. For example, 1/2 + 1/2 == 1 will return 0, which means that the equal-to relationship does not hold. This is because the truncation of the integer division--that is,1/2 produces 0, not 0.5. For the same reason, 1.0/3/0 + 1.0/3/0 + 1.0/3.0 == 1.0 will return 0. Cast operator: convert one data type to a different one by prefixing the case operator. Syntax or general form: (data-type)x. Here x is a variable(or, expression) that contains the value of the current data type. Looping, or iteration, is used in programing to perform the same set of statements over and over untill certain specified conditions are met. The for statement first evaluates expression 1, which usually initialized one or more variables. In other words, expression 1 is only evaluated once when then for statement is first encounted. The second expression, expression 2, is the conditional part that is evaluated right after the evaluation of expression 1 and then is evaluated after each successful looping by the for statement. If expression 2 returns a nonzero value, the statements within the braces are executed, Usually, the nonzero value is 1, If expression 2 returns 0, the looping is stopped and the execution of the for statement is finished. The third expression in the for statement, expression 3, is not evaluated when the for statement is first encounted. However, expression 3 is evaluated after each looping and before the statement goes back to test expression 2 again. The null statement: For example for(i=1;i<9;i++); means for(i=1;i<9;i++) ; Compare the following two: 1. for(i=1;i<9;i++); sum += i; 2. for(i=1;i<9;i++) sum += i; The result is not same. The null statement is perfectly legal in C. You should pay attention to placing semicolons in your for statement. Playing with an Infinite Loop: for(;;){ /* statement block*/ } There're no expressions in the three expression fields. The statements inside the statement block will be executed over and over without stopping. While (1){ /* statement block*/ } --> Note, there’s NOT semicolon here.

do{
/* statement block*/
} while(expression); –> Note, there’s semicolon here.
The do-while statement ends with a semicolon which is an important distinction form the if and while statement. The for statement does not end with semicolon.

How do you know the size of a data type on your machine? sizeof(expression)
Here expression is the data type or variable whose size is measured. The value of the size is returned ,in writes of bytes.
size = sizeof(int)
sizeof(char)
sizeof(float)
sizeof(double)

&& Logical AND
|| Logical OR
! Logical negation

In C, if an expression or operator returns a nonzero value, the expression return TRUE.If and expression or operator return 0, the expression return FALSE. In other words, TRUE can be used to represent any nonzero value returned by an expression or operator;FALSE is equivalent to 0.

分类: 科技 标签:

CTDB Introduction

2007年8月17日 15点19分 没有评论

CTDB, Cluster Trival Database.
1. Consistent data and consistant locking
2. If one node failed, CTDB will automatically recovery and repair all CTDB database.
3. CTDB is PCIFS, parallel CIFS with samba 3/4
4. Support TCP and Infiniband

Requirement:
This filesystem must be mounted and available on all nodes in the CTDB cluster.

HA Features:
The CTDB nodes in a cluster designates one nodes as recovery master through an election process. If the recovery master node failes a new election is initiated so that the cluster will always guarantee there will be a recovery master. The recovery master will continously monitor the cluster to verify that all nodes contain a consistant configuration and view of the cluster and will initiate a recovery process when required.

During the recovery phase, the recovery master will automatically rebuild/recovery all clustered TDB database to ensure that the database are consistent. Recovery typically takes between 1 and 3 seconds. During the recovery period the database are frozen, and all database IO operation by CTDB clients are suspend.

To speed up the process of IP takeover and when clients attached to a failed node recovery as fast as possible. CTDB will automatically generate gratutions ARP packets to inform all nodes of the changed MAC address for that IP. CTDB will also send ‘tickle ack’ packets to all attached clients to trigger that clients to immediatlly recognized that the TCP connection needs to be re-established and to shortcut any TCP restransmission time outs that may be active in the clients.

CTDB current Architecture:
1. Multi-process server:
Multiple smbd daemons, one per client
Each daemon attached to a number of database
Database store all shared meta_data
2. Clustering this should be easy:
Why not just use a cluster database
Each smbd talkes to cluster database instead of local database
Obvious solution can be wrong.

CTDB is similarin conceps to Berkeley DB.

分类: 科技 标签:

Mandriva 2007: KMPlayer play rmvb

2007年8月4日 10点53分 没有评论

I installed Mandriva 2K7 in my home PC. Fix the problem using KMPlay to play rmvb media. KMplayer can support the following types: GStream, xine and MPlayer. First, xine and Mplayer are able to play Mpeg format. When I try to open a rmvb file, Mplayer has only the sound without pictures, and it seems that xine does not support realplayer media.

As google suggestion, I set plf as Mandriva update source, and issue the below commands:

# urpmi xine-faad
# urpmi xine-flac
# urpmi real-codecs
# urpmi win32-codecs

Only flad can be installed,”xine-faad”,”real-codecs” and “win32-codecs” packagea are not found in the plf source. Hmm, I have to manually download the relevant packages from “rpm.pbone.net”. Note, “libstdc++5-3.3.6-3mdk.i586.rpm” is needed before install real-codes and win32-codes.

#wget ftp://mandrake.contactel.cz/plf/mandrake/2007.1/non-free/release/binary/i586/real-codecs-1.2-3plf.i586.rpm
#wget ftp://mirror.switch.ch/mirror/mandrake/official/2007.0/extra/i586/libstdc++5-3.3.6-3mdk.i586.rpm
#wget ftp://mandrake.contactel.cz/plf/mandrake/2007.0/non-free/release/binary/i586/win32-codecs-1.8-1plf2007.0.i586.rpm

# rpm -ivh libstdc++5-3.3.6-3mdk.i586.rpm
# rpm -ivh real-codecs-1.2-3plf.i586.rpm
# rpm -ivh win32-codecs-1.8-1plf2007.0.i586.rpm

OK, the KMplayer can play rmvb now.However, Mplayer trouble still exists.

分类: 科技 标签:

Alexandre Pato To Milan!

2007年8月3日 9点32分 没有评论

From www.acmilan.com 8/2/2007
MILANO – The great talent Alexandre Pato will train with Milan and will be able to play the friendly matches starting from the 3rd of September 2007. From teh 3rd of January 2008 onwards he will also be able to play in the official games.

分类: 科技 标签:

Today of last year

2007年7月31日 13点35分 没有评论

I went to work at Plasmon,Zhuhai in today of last year.
This 8 months experience is very senseful for myself development.

分类: 科技 标签:

Monthly Summary

2007年7月30日 17点52分 没有评论

Study on CentOS 4.5 new features (Cluster Suite and Global File System) from the beginning of July, and finnally submit a detail Feasibility Study report at July 20th, the today before my birthday. This researching document indicating how to configure failover NFS(both active/passive and active/active) and GFS performance tunning, can greatly help our team in the next generation software. However, Samba failover across cluster has not been fixed.

Last week, I leaded three Engineers(Lin Peng, Hou Xianghua, Pang Rangsheng) to prepare the machines for UT customer. I learned a lot when working with this new group, and the most important thing is I must let every member in my team know his responsibilty. I act as directer, who stand in center to help the team accomplish the works. Second, make clear conclusion, continute or cancel the test, always focus on the target that these machines need be send to customer at Friday. Always encourage the team, calmly analyze the meeting problems and try to resolve it bravely. Respect everyone, and have good communication. In effection, clearly mind, strong decision and bravery are three key points.

In the following spare time, I think I should enhance the program ability and know deeply about computer networking. I plan to review C program at home, and if I have free time in office, I’d like to the book named “Computer Networking”. This study scheme is arranged to be accomplished in August.

Phillip, you have been 28 years old. Keep studing, never give up.

分类: 科技 标签: