• <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

            蹣跚前行者

            常用鏈接

            統(tǒng)計(jì)

            Algorithms

            C++

            最新評論

            深入理解strcpy,strncpy

            ~對初學(xué)者屬于深入理解吧,高手請勿在此浪費(fèi)寶貴時間~

            看到這樣一個改錯題:
            char p[5]; 
            char* s="12345"
            strcpy(p,s);
               cout << p << endl;
            錯誤之處是很顯然的,作為字符串字面值的"12345"會在結(jié)尾處自動加上空字符null,從而長度是6,上面的strcpy是會越界的,從而是個越界錯誤。
            問題是我以為這樣的程序編譯的時候會出錯呢!但是我在vc上運(yùn)行時是可以輸出正確結(jié)果的,這讓我很是納悶!后來找到了strcpy的源代碼才知原因,strcpy函數(shù)是不進(jìn)行越界處理的. 又因?yàn)閟trcpy連null一起復(fù)制,從而p能輸出正確的結(jié)果"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雖然不進(jìn)行越界處理,仍然是個挺好的函數(shù)呢,但是注意了,下面的代碼就能暴露出strcpy 的缺點(diǎn)了.
            char p[5];
            char *= "12345678";
            strcpy(p,s);
            cout 
            << p <<endl; //輸出12345678,而不是我們所設(shè)想的12345
            為了不因不知s的長度而犯下錯誤,推薦使用strncpy。但是是不是用了strncpy就萬無一失了呢?看下面的代碼:
            char p[5];
            char* s = "12345";
            strncpy(p,s,
            5);
            cout 
            << p <<endl; //12345*&^(后面表示亂碼)
            不是都限制個數(shù)了么?為什么后面又有亂碼?
            問題來自在上述情形strncpy是不會復(fù)制字符串s后面的null的,不是以null結(jié)束的p不會輸出正確結(jié)果的。
            仍然給出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這么不穩(wěn)定,為何用它?strncpy經(jīng)常用于修改一個長字符串中間的一部分(看出不復(fù)制null的原因了吧!),如果實(shí)在要用到上述的代碼上,就在代碼最后加上p[5] = '\0'; 吧!

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

            評論

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

            不是strncpy不穩(wěn)定
            數(shù)組長度不夠,讓strncpy越界再加個\0 ?
              回復(fù)  更多評論   

            99久久成人国产精品免费| 综合久久精品色| 国产福利电影一区二区三区,免费久久久久久久精 | 亚洲AV日韩精品久久久久久久| 久久久久免费精品国产| 香蕉久久AⅤ一区二区三区| 青青草国产成人久久91网| 久久九九久精品国产| 久久久精品2019免费观看| 久久久久九国产精品| 热99re久久国超精品首页| 久久天天躁狠狠躁夜夜avapp| 国产91久久综合| 青青草原综合久久大伊人精品| 亚洲色欲久久久久综合网 | 一本色综合久久| 超级碰久久免费公开视频| 久久亚洲精品视频| 欧美综合天天夜夜久久| 久久亚洲国产中v天仙www| 香港aa三级久久三级| 国内精品久久久久久久影视麻豆| 久久无码av三级| 中文字幕久久精品 | 亚洲午夜久久久精品影院| 伊人色综合久久天天| 香蕉99久久国产综合精品宅男自 | 久久精品国产亚洲精品2020| 久久一区二区免费播放| 香蕉久久影院| 亚洲v国产v天堂a无码久久| 亚洲va久久久噜噜噜久久男同 | 久久伊人亚洲AV无码网站| www.久久热.com| 色偷偷88888欧美精品久久久 | 天天爽天天爽天天片a久久网| 久久人人爽人人爽人人片AV不| 综合久久一区二区三区| 一本久道久久综合狠狠躁AV| 狠狠综合久久综合中文88| 88久久精品无码一区二区毛片 |