• <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的原因了吧!),如果實在要用到上述的代碼上,就在代碼最后加上p[5] = '\0'; 吧!

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

            評論

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

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

            国产精品久久久久久久久免费| 天天爽天天狠久久久综合麻豆| 久久亚洲高清观看| 久久精品国产精品亜洲毛片| 国产精品久久久久久久app| 久久综合给合久久狠狠狠97色69| 久久er国产精品免费观看2| 亚洲国产天堂久久综合| 久久精品极品盛宴观看| 久久精品国产精品青草| 亚洲?V乱码久久精品蜜桃 | 久久成人影院精品777| 色综合合久久天天给综看| 久久亚洲中文字幕精品有坂深雪| 久久人妻少妇嫩草AV无码蜜桃| 亚洲狠狠婷婷综合久久蜜芽| 国产精品无码久久四虎| 久久水蜜桃亚洲av无码精品麻豆| 久久免费视频6| 亚洲伊人久久大香线蕉苏妲己| 亚洲人成精品久久久久| 色综合久久中文字幕综合网| 女人香蕉久久**毛片精品| 久久亚洲国产成人精品性色| 无码任你躁久久久久久久| 狠狠色综合网站久久久久久久| 欧美va久久久噜噜噜久久| 久久人妻AV中文字幕| 一本综合久久国产二区| 久久久久亚洲av成人无码电影| 国产精品18久久久久久vr| 久久精品麻豆日日躁夜夜躁| 新狼窝色AV性久久久久久| 亚洲国产精品无码久久一线 | 久久综合久久鬼色| 久久av免费天堂小草播放| 狠狠久久综合伊人不卡| 久久久久久狠狠丁香| 51久久夜色精品国产| 久久久久亚洲av毛片大| 热综合一本伊人久久精品|