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

            最新評(píng)論

            深入理解strcpy,strncpy

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

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

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

            評(píng)論

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

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

            国产综合久久久久| 99精品国产99久久久久久97 | 免费精品久久久久久中文字幕 | 国产精品久久永久免费| 国产美女亚洲精品久久久综合| 亚洲综合久久久| 精品伊人久久大线蕉色首页| 亚洲精品成人久久久| 久久只这里是精品66| 18禁黄久久久AAA片| 亚洲伊人久久精品影院| 亚洲熟妇无码另类久久久| 久久精品国产亚洲av麻豆小说| 国产成人精品免费久久久久| 久久九九有精品国产23百花影院| 亚洲一区中文字幕久久| 精品国产一区二区三区久久蜜臀| 久久精品国产精品亚洲人人| 狠狠色丁香久久婷婷综合图片| 人妻无码精品久久亚瑟影视 | 中文字幕亚洲综合久久菠萝蜜| 国产一区二区久久久| 人妻少妇久久中文字幕| 99久久精品久久久久久清纯| 伊色综合久久之综合久久| 久久精品国产亚洲AV高清热 | 国产亚洲婷婷香蕉久久精品| 国产精品成人99久久久久| 久久天天躁夜夜躁狠狠| 久久不射电影网| 亚洲欧美精品一区久久中文字幕 | 狠狠色丁香婷婷久久综合不卡| 国产亚洲美女精品久久久| 久久天天躁狠狠躁夜夜2020一| 久久精品国产精品青草app| 一本一道久久a久久精品综合| 久久久无码精品亚洲日韩按摩 | 久久91精品国产91久久小草| 久久99亚洲综合精品首页| 亚洲第一极品精品无码久久| 久久精品成人|