zoukankan      html  css  js  c++  java
  • 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;

    }

  • 相关阅读:
    笔记-归并排序
    Repeated Substring Pattern
    Assign Cookies
    Number of Boomerangs
    Paint Fence
    Path Sum III
    Valid Word Square
    Sum of Two Integers
    Find All Numbers Disappeared in an Array
    First Unique Character in a String
  • 原文地址:https://www.cnblogs.com/embeddedking/p/9656642.html
Copyright © 2011-2022 走看看