Thursday, November 29, 2007

How to determine the size of an array

  1. sizeof a/sizeof *a (btw, sizeof is an operator instead of a function)

Monday, November 26, 2007

Some problems...

// there are two temporary objects to be de-constructed
1. class Person
{
static int num;
public:
Person() { num++; }
~Person() { num--; Print(); }
void Print() { cout<<"The person num is: "<< num << endl; }
};
int Person::num=0;
Person foo(Person p){ p.Print(); return p;}
void main()
{
Person p1;
p1.Print();
Person p2=foo(p1);
p2.Print();
return;
}

// when resize an array, delete the old array before assign a new one
2. void test(int *&array)
{
int *var = new int[20]; for (int i=0;i<10;i++)
{ var[i]=array[i]+10; }
for (int i=10;i<20;i++) var[i]= i * 3;
if(array) delete[] array; array=var;
}
int main()
{
int *var = new int[10];
for (int i=0;i<10;i++) var[i]=i;
test(var);
for (int i=0;i<20;i++) cout << var[i] << "\n";
delete [] var;
}

Sunday, November 25, 2007

Easy Forgotten Points

1. switch-case are based on integer-valued sentence

Notes on Programming Style

1. Less use of C/C++ standard library functions, for easy transplantation.

2.

Notes on Data Type

1. void

2. null

3. bool: true/false

------------------------

4. char - 8 bit(1 byte); int - 2 bytes; float; double;
max/min number are defined in limits.h/float.h
short int, int, long int, float, double, long double
signed/unsigned(signed--forced to use a sign)
2e-2: the base e means 10

Saturday, November 24, 2007

Notes on reading "Thinking in C++"

I am reading the book Thinking in C++ these days. I will write down some important points in this post during my reading.

1. Difference on overloading and overriding:
Overriding - same methods with same arguments and same return types associated in a class and its subclass
Overloading - same methods with different arguments, may or may not be same return type written in the same class itself
-------------------------------------
Example for overriding
Class A { Virtual void hi(int a) { }}
Class B: public A{ public overrid void hi(int a){ }}

Example for overloading
Class A {a(){} a(int a){}}
-------------------------------------

2. two ways to re-use the codes: compoition and inheritance

3. Three points on OOP: abstract data type, inheritance, polymorphism

4. Precedure of compilation: (1) pre-processing (2) compilation: (a) syntax analysis->tree structure (b) code generator->assemble code/machine code (get .obj) (3) linker. Some points: global optimizer, peephole optimizer, static type checking, dynamic type checking

5. All C++ library are in name space of std

6.

Sunday, November 11, 2007

Some notes on C/C++

1. you can create a file with ofstream in a c/cpp programe, but you cannot create a directory such way

Problems to be thinking

1. why mysql comes out a problem with "release mode", say not start with "/"

2. why when there is directory, there may be problem with compling

Saturday, November 10, 2007

Some notes on MySQL

1. how to check whether a field is NULL?
First, NULL in MySQL is not a value, it is a condition. So if you try and write something "= NULL" you'll get an empty set rather than the results you're looking for. Try "IS NULL" instead. For example, mysql> select * from demo_people left join demo_property on demo_people.pid = demo_property.pid where spid is NULL;

2. becaulful with this code:
while((row=mysql_fetch_row(res)))
{
for(int j=0;j {
printf("%s ",row[j]);
}
printf("\n");
}
if the MySQL query get a tuple whchi has field "NULL", then problem comes!

MySQL vs C/C++

1. it seems row[0] (MYSQL_ROW row) is a char*, great