9月6日,网新恒天2014校园招聘笔试编程题

  已知memcpy的函数为: void* memcpy(void *dest , const void* src , size_t count)其中dest是目的指针,src是源指针。不调用c++/c的memcpy库函数,请编写memcpy。

  点评:老题,参考答案如下

  [cpp] view plaincopyprint?

  //copyright@July 2013/9/24

  void* memcpy(void *dst, const void *src, size_t count)

  {

  //安全检查

  assert( (dst != NULL) && (src != NULL) );

  unsigned char *pdst = (unsigned char *)dst;

  const unsigned char *psrc = (const unsigned char *)src;

  //防止内存重复

  assert(!(psrc<=pdst && pdst

  assert(!(pdst<=psrc && psrc

  while(count--)

  {

  *pdst = *psrc;

  pdst++;

  psrc++;

  }

  return dst;

  }

  //copyright@July 2013/9/24

  void* memcpy(void *dst, const void *src, size_t count)

  {

  //安全检查

  assert( (dst != NULL) && (src != NULL) );

  unsigned char *pdst = (unsigned char *)dst;

  const unsigned char *psrc = (const unsigned char *)src;

  //防止内存重复

  assert(!(psrc<=pdst && pdst

  assert(!(pdst<=psrc && psrc

  while(count--)

  {

  *pdst = *psrc;

  pdst++;

  psrc++;

  }

  return dst;

  }