顯示具有 programming 標籤的文章。 顯示所有文章
顯示具有 programming 標籤的文章。 顯示所有文章

星期五, 2月 06, 2009

upnp compilation with error

編繹 upnp 相關的程式時出現
upnp/ithread.h:153: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'ithread_rwlockattr_t'


解決方法是
加上
-D_GNU_SOURCE

如:

$(CC) $(CFLAGS) $(INCLUDES) -c $<
$(CC) $(CFLAGS) $(INCLUDES) -D_GNU_SOURCE -c $<




參考資料:
https://dev.openwrt.org/ticket/3181

星期一, 1月 19, 2009

NULL to strlen function

當我們對某個指標做 strlen,且該指標指向 NULL 時,例如:
char *ptr = NULL
printf("string length : %d\n", strlen(ptr));

會產生
Segmentation fault

因為:
http://bytes.com/groups/c/586992-null-strlen-function#post2303741
You're apparently a new programmer, so let me add this:

A NULL pointer and a string of length zero are not the same thing.

A NULL pointer traditionally has the value of 0, which is usually an
unsuitable pointer; but in any case it is a reserved value defined to be a
non-functional pointer.

A string of length 0 has a non-NULL pointer, but the area of memory that is
pointed to has its first byte as 0.

The two are very different.

解法是先檢查指標是否為 NULL:
char *ptr = NULL;
if(ptr != NULL)
printf("string length : %d\n", strlen(ptr));
else
printf("string is a NULL string\n");

星期三, 9月 10, 2008

malloc memset free

用於字串指標:
char *ptr;
ptr = (char *) malloc(sizeof(char) * NumberOfElement);

memset(ptr, '\0', sizeof(ptr));
...
free(ptr);


用於變數指標:
int *ptr
ptr = (int *) malloc(sizeof(char) * NumberOfElement);

memset(ptr, 0, sizeof(ptr));
...
free(ptr);

星期三, 7月 23, 2008

suggest parentheses around assignment used as truth value

原文出處
http://darkmomo.blogspot.com/2008/05/suggest-parentheses-around-assignment.html

而在C語言中,"非0為true'的哲學下",
int rxDataSize=0;
if(rxDataSize=0)會通過檢查;
但是在g++中,"非0為true'的哲學下"已不適用,必須正確指出ture or false,
即if((rxDataSize=0)) 才會通過不會有warring產生。

星期五, 5月 09, 2008

javascript error handler

將下列 加到 區段中

onerror = handleErr;
var txt="";
function handleErr(msg,url,l){
txt="Error: " + msg + "\n";
txt+="url: " + url + "\n";
txt+="Line: " + l + "\n";
alert(txt);
return true;

星期三, 3月 26, 2008

標準前置處理巨集

標準前置處理巨集

1. 功能:

每一個標準前置處理巨集名稱都以兩個底線字元開頭,再以兩個底線字元結束,主要作用是用來反應程式編譯時的資訊。

2. 種類:

巨集名稱 說明 輸出型態
_ _LINE_ _ 目前檔案中的行號 整數
_ _FILE_ _ 原始檔案的名稱 字串
_ _DATE_ _ 原始檔案的編譯日期 字串
_ _TIME_ _ 原始檔案的編譯時間 字串

3. 範例



* 範例一

Cout << _ _LINE_ _ //此為目前檔案中的第 3 行

Cout << _ _FILE_ _ //檔案名稱為 d:\csie25\chp5\ex5_5.cpp

Cout << _ _DATE_ _ //程式編譯日期為 May 14 2003

Cout << _ _TIME_ _ //程式編譯時間為 11:36:35