C語言實現memcpy

來源:互聯網
上載者:User
 c語言實現memcpy

今天到I 公司去面試,面試方式比較特殊,沒有筆試,就是2 個面試官,一人一句輪番發問,涉及面很廣,涉及到作業系統(MMU 、page out 、process/thread 、semaphore 、interrupt), OOP( 多態、design pattern) 、資料結構( 排序、二叉尋找樹) 、電腦網路(OSI 5 層) 、C 語言(big/small endian) 、英語口語等等,問了大約一個小時左右。

所有問題都是口頭表述,只在紙上寫了一個memcpy 程式,用C 語言實現,腦子一發矇,既然寫成了strcpy ,真該死。

回家了查詢了一下memcpy 定義,如下:

Void *memcpy(void *dest, const void *src, unsigned int count);

查詢msdn, 發現Remark 如下:

memcpy copies count bytes from src to dest ; If the source and destination overlap, the behavior of memcpy is undefined. Use memmove to handle overlapping regions.

以上描述針對dest 和 src 所指的記憶體位址有重疊的情況,記憶體位址重疊情況,memcpy 函數處理步驟未定,而memmove 對重疊情況給予處理;

在winXP+visual c++2005 測試 memcpy 函數,程式如下:

 

#include "stdafx.h"

#include <string.h>

int _tmain (int argc , _TCHAR * argv [])

{

       char s [16] = "aabbcc" ;

       char d [16] = {0};

      

       memcpy (s +2, s , 4);

       printf ("%s" , s );

       return 0;

}

結果輸出 “aaaabb”, 由此可見windows 平台的c 運行時MSVCRT 的memcpy 函數對重疊部分做了處理,同memmove 的實現。//notes: 如果重疊部分不做處理,應該輸出”aaaaaa”

下面我們用c 語言來實現memcpy 函數, 首先我們寫出不對記憶體重疊的處理函數,如下:

void *memcpy_no_handle_overlap (void *dest , void *src , unsigned int count )

{

       if ((NULL ==dest ) || (NULL ==src ))

              return NULL ;

 

       char *d = (char *)dest ;

       char *s = (char *)src ;

 

       //Do normal (Upwards) Copy

       while (count -- > 0)

              *d ++ = *s ++;

       return dest ;

}

測試程式如下:

int _tmain(int argc, _TCHAR* argv[])

{

       char s[16] = "aabbcc";

       char d[16] = {0};

      

       memcpy_no_handle_overlap(s+2, s, 4);

 

       printf("%s", s);

       return 0;

}

輸出結果”aaaaaa”

下面討論處理memory overlapping 情況,如:

判斷overlapping 條件如下:

If ( (dest <= src) ||                // green region 1

   (dest >=src+count) )           // green region 2

{

       // no memory overlapping

}

Else  // red region 3

{

       // there is overlapping

}

Overlapping 的處理:

我們可以看到memcpy_no_handle_overlap 函數,是從低地址依次賦值到高地址;在處理overlapping 時,如果我們採用同樣的方法( 低地址到高地址) ,高地址的值將會被覆蓋,所以我們應該從高地址依次到低地址賦值,如:

 

  函數代碼如下:

void *memcpy_handle_overlap(void *dest, void *src, unsigned int count)

{

       if ((NULL==dest) || (NULL==src))

              return NULL;

 

       char *d = (char *)dest;

       char *s = (char *)src;

 

       //Check for overlapping buffers:

       if ( (d<=s) || (d>=s+count) )

       {     

              //Do normal (Upwards) Copy

              while (count-- > 0)

                     *d++ = *s++;

       }

       else

       {

              //Do Downwards Copy to avoid propagation

              while (count > 0)

              {

                     *(d+count-1) = *(s+count-1);

                     --count;

              }

 

       }

 

       return dest;

}

測試代碼:

int _tmain(int argc, _TCHAR* argv[])

{

       char s[16] = "aabbcc";

       char d[16] = {0};

      

       memcpy_handle_overlap(s+2, s, 4);

 

       printf("%s", s);

       return 0;

}

輸出結果為: “aaaabb “

 

最後測試代碼如下:

int _tmain(int argc, _TCHAR* argv[])

{

       char s[16] = "aabbcc";

       memcpy_no_handle_overlap(s+2, s, 4);

       printf("memcpy(ignore memory overlapping): %s/n", s);

 

       strcpy(s, "aabbcc");

       memcpy_handle_overlap(s+2, s, 4);

       printf("memcpy(handle memory overlapping): %s/n", s);

 

     strcpy(s, "aabbcc");

       memcpy(s+2, s, 4);

       printf("memcpy( MSVCRT ): %s/n", s);

 

       strcpy(s, "aabbcc");

       memmove(s+2, s, 4);

       printf("memmove( MSVCRT): %s/n", s);

 

       return 0;

}

輸出結果為:

memcpy(ignore memory overlapping): aaaaaa

memcpy(handle memory overlapping): aaaabb

memcpy( MSVCRT ): aaaabb

memmove( MSVCRT): aaaabb

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.