小甲鱼 发表于 2016-12-16 16:47:49

memmove -- 拷贝内存空间

memmove 函数文档

函数概要:

memmove 函数从 src 指向的内存空间拷贝 n 个字节到 dest 指向的内存空间。为了避免因为两个内存空间出现重叠而导致的悲剧,该函数会先将 src 指向的内存空间拷贝到一个临时的数组中,完了再从临时数组拷贝到目标内存空间。


函数原型:

#include <string.h>
...
void *memmove(void *dest, const void *src, size_t n);

参数解析:


参数 含义
dest指向目标内存空间
src指向源内存空间
n指定要拷贝到 dest 指向空间的前 n 个字节


返回值:

返回 dest 指向的内存空间。


演示:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
      char str[] = "I love FishC.com!";
      char *ptr;
      int length = sizeof(str);

      printf("length = %d\n", length);

      ptr = (char *)malloc(length * sizeof(char));
      if (ptr == NULL)
      {
                exit(1);
      }

      memset(ptr, 0, length);
      memmove(ptr, str, length);

      printf("%s\n", ptr);

      return 0;
}


anzhexuan666 发表于 2022-3-23 10:50:39

沙发
页: [1]
查看完整版本: memmove -- 拷贝内存空间