• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            liyuxia713

            蹣跚前行者

            常用鏈接

            統計

            Algorithms

            C++

            最新評論

            深入理解strcpy,strncpy

            ~對初學者屬于深入理解吧,高手請勿在此浪費寶貴時間~

            看到這樣一個改錯題:
            char p[5]; 
            char* s="12345"
            strcpy(p,s);
               cout << p << endl;
            錯誤之處是很顯然的,作為字符串字面值的"12345"會在結尾處自動加上空字符null,從而長度是6,上面的strcpy是會越界的,從而是個越界錯誤。
            問題是我以為這樣的程序編譯的時候會出錯呢!但是我在vc上運行時是可以輸出正確結果的,這讓我很是納悶!后來找到了strcpy的源代碼才知原因,strcpy函數是不進行越界處理的. 又因為strcpy連null一起復制,從而p能輸出正確的結果"12345"
            /*The strcpy function copies strSource, including the terminating null character, to the location specified by strDestination. No overflow checking is performed when strings are copied or appended. The behavior of strcpy is undefined if the source and destination strings overlap.*/
            char * __cdecl strcpy(char * dst, const char * src) 

            char * cp = dst; 

            while*cp++ = *src++ ) 
            /* Copy src over dst */ 

            return( dst ); 
            }
             
            貌似strcpy雖然不進行越界處理,仍然是個挺好的函數呢,但是注意了,下面的代碼就能暴露出strcpy 的缺點了.
            char p[5];
            char *= "12345678";
            strcpy(p,s);
            cout 
            << p <<endl; //輸出12345678,而不是我們所設想的12345
            為了不因不知s的長度而犯下錯誤,推薦使用strncpy。但是是不是用了strncpy就萬無一失了呢?看下面的代碼:
            char p[5];
            char* s = "12345";
            strncpy(p,s,
            5);
            cout 
            << p <<endl; //12345*&^(后面表示亂碼)
            不是都限制個數了么?為什么后面又有亂碼?
            問題來自在上述情形strncpy是不會復制字符串s后面的null的,不是以null結束的p不會輸出正確結果的。
            仍然給出strncpy的源代碼:
            /*The strncpy function copies the initial count characters of strSource to strDest and returns strDest. If count is less than or equal to the length of strSource, a null character is not appended automatically to the copied string. If count is greater than the length of strSource, the destination string is padded with null characters up to length count. The behavior of strncpy is undefined if the source and destination strings overlap.*/
            char * __cdecl strncpy ( char * dest, const char * source, size_t count ) 

            char *start = dest; 

            while (count && (*dest++ = *source++)) /* copy string */ 
            count
            --

            if (count) /* pad out with zeroes */ 
            while (--count) 
            *dest++ = '\0'

            return(start); 
            }
             
            那strncpy這么不穩定,為何用它?strncpy經常用于修改一個長字符串中間的一部分(看出不復制null的原因了吧?。绻麑嵲谝玫缴鲜龅拇a上,就在代碼最后加上p[5] = '\0'; 吧!

            posted on 2009-04-26 20:59 幸運草 閱讀(3358) 評論(1)  編輯 收藏 引用 所屬分類: C++

            評論

            # re: 深入理解strcpy,strncpy 2009-12-12 14:04 小螞蟻

            不是strncpy不穩定
            數組長度不夠,讓strncpy越界再加個\0 ?
              回復  更多評論   

            久久本道伊人久久| 久久最新精品国产| 一本色道久久HEZYO无码| 日本强好片久久久久久AAA| 精品久久久久久综合日本| 思思久久精品在热线热| 狠狠狠色丁香婷婷综合久久五月| 青青热久久国产久精品| 9191精品国产免费久久| 久久夜色精品国产欧美乱| 性做久久久久久免费观看| 久久综合久久综合九色| 中文字幕人妻色偷偷久久| 久久综合久久性久99毛片| 91久久成人免费| 国产精品美女久久久m| 亚洲欧美精品一区久久中文字幕 | 一级做a爰片久久毛片毛片| 热re99久久6国产精品免费| 中文字幕无码av激情不卡久久| 色综合久久精品中文字幕首页| 久久人人爽人人爽人人片AV不| 一本大道久久东京热无码AV| 久久久无码精品午夜| 久久精品国产一区二区三区| 99久久精品费精品国产| 精品久久香蕉国产线看观看亚洲| 亚洲国产精品久久久天堂| 99久久精品国产一区二区 | 国产精品免费久久| 久久亚洲精品中文字幕三区| 日本久久久久亚洲中字幕| 亚洲人成精品久久久久| 久久偷看各类wc女厕嘘嘘| 无码精品久久久天天影视 | 久久精品视频91| 久久综合精品国产一区二区三区| 午夜精品久久久内射近拍高清 | 麻豆成人久久精品二区三区免费| 亚洲精品午夜国产VA久久成人| 中文字幕日本人妻久久久免费|