[轉] strcpy函數的實現

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   使用   ar   strong   

轉自:strcpy函數的實現

代碼實現

#include <iostream>#include <assert.h>#include <iostream>//#include <string.h>using namespace std;int strlen(const char *src){    assert(src != NULL);    int lens = 0;    while (*src++ != ‘\0‘)        lens++;    return lens;}char *memcpy(char *dst, const char *src, int cnt){    assert(dst != NULL && src != NULL);    char *ret = dst;    if (dst >= src && dst <= src + cnt - 1)    {        src += cnt - 1;        dst += cnt - 1;        while (cnt--)            *dst-- = *src--;    }    else    {        while (cnt--)            *dst++ = *src++;    }    return ret;}char *strcpy(char *dst, const char *src){    assert(dst != NULL);    assert(src != NULL);    char *ret = dst;    memcpy(dst, src, strlen(src)+1);    return ret;}int main(){    char a[] = "hello";    char b[] = "b1";    //strcpy(a, b);    strcpy(a, a+1);    char *f = strcpy(a, a+1);    cout << a << endl;    cout << f << endl;    int m = 0;    int n = 5;    int c = (m=n++);    cout << "c:" << c << endl;    cout << "n:" << n << endl;    return 0;}
View Code

已知strcpy函數的原型是:

char *strcpy(char *dst, const char *src);

  1. 實現strcpy函數
  2. 解釋為什麼要返回char *
  3. 假如考慮dst和src記憶體重疊的情況,strcpy該怎麼實現
1.strcpy的實現代碼
char * strcpy(char *dst,const char *src)   //[1]{    assert(dst != NULL && src != NULL);    //[2]    char *ret = dst;  //[3]    while ((*dst++=*src++)!=‘\0‘); //[4]    return ret;}

[1]const修飾

  • 源字串參數用const修飾,防止修改源字串。

[2]null 指標檢查

  • 不檢查指標的有效性,說明答題者不注重代碼的健壯性。
  • 檢查指標的有效性時使用assert(!dst && !src);char *轉換為bool即是類型隱式轉換,這種功能雖然靈活,但更多的是導致出錯機率增大和維護成本升高。
  • 檢查指標的有效性時使用assert(dst != 0 && src != 0);直接使用常量(如本例中的0)會減少程式的可維護性。而使用NULL代替0,如果出現拼字錯誤,編譯器就會檢查出來。

[3]返回目標地址

  • 忘記儲存原始的strdstt值。

[4]‘\0‘

  • 迴圈寫成while (*dst++=*src++);明顯是錯誤的。
  • 迴圈寫成while (*src!=‘\0‘) *dst++=*src++;

迴圈體結束後,dst字串的末尾沒有正確地加上‘\0‘。

2.為什麼要返回char *?

返回dst的原始值使函數能夠支援鏈式運算式。

鏈式運算式的形式如:

int l=strlen(strcpy(strA,strB));

又如:

char * strA=strcpy(new char[10],strB);

返回strSrc的原始值是錯誤的。

其一,源字串肯定是已知的,返回它沒有意義。

其二,不能支援形如第二例的運算式。

其三,把const char *作為char *返回,類型不符,編譯報錯。

3.假如考慮dst和src記憶體重疊的情況,strcpy該怎麼實現

char s[10]="hello";

strcpy(s, s+1); //應返回ello,

//strcpy(s+1, s); //應返回hhello,但實際會報錯,因為dst與src重疊了,把‘\0‘覆蓋了

所謂重疊,就是src未處理的部分已經被dst給覆蓋了,只有一種情況:src<=dst<=src+strlen(src)

C函數memcpy內建記憶體重疊檢測功能,下面給出memcpy的實現my_memcpy。

char * strcpy(char *dst,const char *src){    assert(dst != NULL && src != NULL);    char *ret = dst;    my_memcpy(dst, src, strlen(src)+1);    return ret;}

my_memcpy的實現如下

char *my_memcpy(char *dst, const char* src, int cnt){    assert(dst != NULL && src != NULL);    char *ret = dst;     if (dst >= src && dst <= src+cnt-1) //記憶體重疊,從高地址開始複製    {        dst = dst+cnt-1;        src = src+cnt-1;        while (cnt--)            *dst-- = *src--;    }    else    //正常情況,從低地址開始複製    {        while (cnt--)            *dst++ = *src++;    }        return ret;}

[轉] strcpy函數的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.