strcat小心使用

2021-08-31 07:07:59 字數 868 閱讀 4164

strcat是對字串的拼接,有時候拿過來用挺方便的。但是大面積用,**變得不清晰,都不知道字串到底程式設計了什麼。還有另外一點,如果定義了乙個全域性的字串,函式迴圈裡需要對字串拼接後使用,如果不停的迴圈,就相當於不停的追加,可能造成記憶體溢位,或者資源崩潰。

例子:id; //全域性變數(cstring型別)

//迴圈

char *stemp = id.getbuffer(id.getlength()+1);//cstring轉char*

strcat(stemp, "\r\n");

fwrite(stemp, strlen(stemp1),1,fp);

stemp其實是指向id的字串,id每次都會加乙個回車和換行。

解決方法:

id賦值給乙個區域性變數:

id; //全域性變數(cstring型別)

//迴圈

cstring name = id;

char *stemp =name.getbuffer(name.getlength()+1);//cstring轉char*

strcat(stemp, "\r\n");

fwrite(stemp, strlen(stemp1),1,fp);

補充(2019-02-12):

wcscat是unicode下的函式

用例:

tchar currdir[max_path];

dword dwret = getcurrentdirectory(max_path, currdir);

if (0 == dwret)

else

小心使用BufferedStream

小心使用bufferedstream dotnet下有個bufferedstream類,一些文件說了它的好處,主要是提高效能等等。但好像沒人說它會帶來的問題。我想可能因為大家對裡面的原理了解不深。我綜合了許多資料,發現bufferedstream核心就是要實現批量read和批量write,我不討論w...

使用memset要小心

今天把程式重構了。執行一下,沒想到程式到了退出的時候觸發了乙個 assert 看看棧資訊,執行到了 cstring 的析構函式 cstring cstring freeanyattacheddata afx inlinecstringdata cstring getdata const class ...

實現strcat函式

strcat函式是將兩個字串進行連線。define crt secure no warnings include include include includechar strcat char dest,const char src dest i 0 return dest int main str...