Tuesday, November 27, 2007

troubleshooting

一些troubleshooting的記錄

M$N不能上?
帳號密碼已經確定沒錯, 只差還沒把M$N移掉 重新安裝
檢查一下IE的proxy是否有設定
因為M$N會參考這個設定連上網路
========================================================
while (-1) ; // <-- it's infinite loop
========================================================
char str[]="string";
strlen(str); // 6
sizeof(str); // 7
========================================================
signed char sc8=0x80;
sc >>= 1; // 0xC0
unsigned char
uc8=0x80;
uc >>= 1; // 0x40
========================================================
global 變數重覆定義問題 (未給初始值)
01.c
------------
#include <stdio.h>

char buf[1024];

int main(){}

02.c
-----------
#include <stdio.h>

char buf[1024];

>gcc -o output 01.c 02.c
這時候可以編譯成功, 也可以執行
但會有個風險, 就是2份file, 各自以為這個global變數僅屬於自己擁有而已
因此, 當其中一個改變global變數的內容, 也會反應到另一個上面

解決辦法
1) 加static在global變數之前
2) gcc -fno-common
========================================================
fgets的問題

避免用fgets抓binary的資料, 因為這個函式會預設抓出一行字串出來, 也就是遇到"換行字元", "檔案結尾", 或"達到給定字數"之後, 就會跳出, 而binary檔, 可能會含0x0A, 這會使得程式增加更多不確定性.
建議用fread()或read()代替
========================================================
sol1,
while ( curr=head; curr; curr=curr->next) {
del(curr);
}
sol2,
while ( curr=head; curr; curr=next ) {
next=curr->next;
del(curr);
}
sol1的問題在於下一個node會在del()時就被修改, 導致要取下一個node,造成問題
sol2可以解決這個問題

========================================================

int opt;
while ( (opt=getopt(argc, argv, ""))>0 ) {
...
}

opt must be decleared as integer.
When opt declared as char, the loop will not terminate.