memcpy 的內存拷貝函數


 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 void *memory(void *dst,const void *src,size_t s)
 6 {
 7 
 8    const char* psrc=static_cast<const char*>(src);
 9     char* pdst=static_cast<char*>(dst);
10 
11     if(psrc==NULL||pdst==NULL)
12         return NULL;
13 
14     if(pdst>psrc&&pdst<(psrc+s))
15     {
16         for(size_t i=s-1;i!=-1;i--)
17             pdst[i]=psrc[i];
18     }
19     else
20     {
21         for(size_t i=0;i<s;++i)
22             pdst[i]=psrc[i];
23     }
24     return dst;
25 }
26 int main()
27 {
28    char buf[100]="abcdefghijk";
29    cout << buf << endl;
30    memory(buf+2,buf,5);
31    cout << buf;
32    return 0;
33 }

 函數memcpy是實現,主要是對函數memcpy的理解透徹,防止內存自己拷貝自己,存在從前向后拷貝和從后向前拷貝的順序。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM