memcpy实现
Posted embeddedking
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了memcpy实现相关的知识,希望对你有一定的参考价值。
#include <iostream>
using namespace std;
#include <assert.h>
void* myMemcpy(void* dst, const void* src, size_t count);
int main(void)
{
int test[4] = {1,2,3,4};
int* src = test;
int* dst = NULL;
dst = (int*)malloc(sizeof(int)*4);
if(NULL == dst)
{
cout << "malloc memory failes" << endl;
return 0;
}
for(int i=0; i<4;i++)
{
*(dst+i)=0;
}
myMemcpy(dst, src, 4);
for(int i=0; i<4; i++)
{
cout << *(dst+i) << endl;
}
return 0;
}
void* myMemcpy(void* dst, const void* src, size_t count)
{
assert(dst != NULL && src != NULL);
int nuchunks = count/sizeof(dst);//按CPU位宽拷贝
int slices = count%sizeof(dst);//剩余的按字节拷贝
(unsigned long*)tmpDst = (unsigned long*)dst;
(unsigned long*)tmpSrc = (unsigned log*)src;
while(nuchunks--)
{
*tmpDst++ = *tmpSrc++;
}
while(slices--)
{
*((char*)tmpDst++) = *((char*)tmpSrc++);
}
return (void*)dst;
}
以上是关于memcpy实现的主要内容,如果未能解决你的问题,请参考以下文章