带你全面了解四大内存操作函数memset(),memcpy(),memmove(),memcmp()(附模拟实现)

简介: 带你全面了解四大内存操作函数memset(),memcpy(),memmove(),memcmp()(附模拟实现)

内存操作函数



注:点击蓝色标题可以跳转到官方网站查看更权威的解析哦。

memcpy

void * memcpy ( void * destination, const void * source, size_t num );
  • 函数memcpy从source的位置开始,向后复制num个字节的数据到destination的内存位置
  • 这个函数在遇到‘\0’并不会停下来
  • 如果source和destination有任何重叠,复制的结果都是未定义的

具体使用:

#include<stdio.h>
int main()
{
  int num1[20] = { 0 };
  int num2[] = { 1,2,3,4,5 };
  memcpy(num1, num2, sizeof(num2));
  for (int i = 0; i < sizeof(num1) / sizeof(int); i++)
    printf("%d ", num1[i]);
  printf("\n");
  return 0;
}

模拟实现:

#include<stdio.h>
void* my_memcpy(void* destination, const void* source, size_t num)
{
  void* head = destination;
  while (num--)
  {
    *(char*)destination = *(char*)source;
    ((char*)destination)++;
    ((char*)source)++;
  }
  return head;
}
int main()
{
  int num1[20] = { 0 };
  int num2[] = { 1,2,3,4,5 };
  my_memcpy(num1, num2, sizeof(num2));
  for (int i = 0; i < sizeof(num1) / sizeof(int); i++)
    printf("%d ", num1[i]);
  printf("\n");
  return 0;
}

memmove

void * memmove ( void * destination, const void * source, size_t num );
  • 和memcopy的差别就是memmove函数处理的内存块是可以重复的
  • 如果源空间和目标空间出现重复,就得使用memmove函数处理
  • 可以说,memmove比memcpy只强不弱

具体使用:

#include<stdio.h>
int main()
{
  int num[] = { 1,2,3,4,5,6,7,8,9,10 };
  memmove(num + 2, num, 5 * sizeof(int));
  for (int i = 0; i < sizeof(num) / sizeof(int); i++)
    printf("%d ", num[i]);
  printf("\n");
  return 0;
}

模拟实现:

#include<stdio.h>
void* my_memmove(void* destination, const void* source, size_t num)
{
  void* head = destination;
  if (destination < source)
  {
    while (num--)
    {
      *(char*)destination = *(char*)source;
      ((char*)destination)++;
      ((char*)source)++;
    }
  }
  else
  {
    while (num--)
    {
      *((char*)destination + num) = *((char*)source + num);
    }
  }
  return head;
}
int main()
{
  int num[] = { 1,2,3,4,5,6,7,8,9,10 };
  my_memmove(num, num + 2, 5 * sizeof(int));
  for (int i = 0; i < sizeof(num) / sizeof(int); i++)
    printf("%d ", num[i]);
  printf("\n");
  return 0;
}

memcmp

int memcmp ( const void * ptr1, const void * ptr2, size_t num );
  • 比较从ptr1和ptr2指针开始的num个字节
  • 返回值如下:

具体使用:

#include<stdio.h>
int main()
{
  int num1[] = { 1,2,3,4,5,6 };
  int num2[] = { 1,2,3,4,5,7 };
  if (memcmp(num1, num2, 6 * sizeof(int)) > 0)
    printf(">\n");
  else if (memcmp(num1, num2, 6 * sizeof(int)) < 0)
    printf("<\n");
  else
    printf("=\n");
  return 0;
}

模拟实现:

#include<stdio.h>
int my_memcmp(const void* ptr1, const void* ptr2, size_t num)
{
  while (num-- && *(char*)ptr1 == *(char*)ptr2)
  {
    ((char*)ptr1)++;
    ((char*)ptr2)++;
  }
  if (num + 1 == 0)
    return 0;
  else if (*(char*)ptr1 > *(char*)ptr2)
    return 1;
  else
    return -1;
}
int main()
{
  int num1[] = { 1,2,3,4,5,8 };
  int num2[] = { 1,2,3,4,5,7 };
  if (my_memcmp(num1, num2, 6 * sizeof(int)) > 0)
    printf(">\n");
  else if (my_memcmp(num1, num2, 6 * sizeof(int)) < 0)
    printf("<\n");
  else
    printf("=\n");
  return 0;
}

memset

void * memset ( void * ptr, int value, size_t num );
  • Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char).——将 ptr 指向的内存块的第一个字节数设置为指定值(解释为无符号字符)。
  • 即从 ptr 指向的内存块的第一个字节开始,将每个字节的值设为value,直至完成第num个字节的赋值。
  • 一般用于将数组初始化为0

具体使用:

#include<stdio.h>
int main()
{
  int nums[5] = { 1,1,1,1,1 };
  memset(nums, 0, sizeof(nums));
  for (int i = 0; i < sizeof(nums) / sizeof(int); i++)
    printf("%d ", nums[i]);
  printf("\n");
  memset(nums, 1, sizeof(nums));
  for (int i = 0; i < sizeof(nums) / sizeof(int); i++)
    printf("%d ", nums[i]);
  printf("\n");
  return 0;
}

模拟实现:

#include<stdio.h>
void* my_memset(void* ptr, int value, size_t num)
{
  void* head = ptr;
  while (num--)
  {
    *(char*)ptr = value;
    ((char*)ptr)++;
  }
  return head;
}
int main()
{
  int nums[5] = { 1,1,1,1,1 };
  my_memset(nums, 0, sizeof(nums));
  for (int i = 0; i < sizeof(nums) / sizeof(int); i++)
    printf("%d ", nums[i]);
  printf("\n");
  my_memset(nums, 1, sizeof(nums));
  for (int i = 0; i < sizeof(nums) / sizeof(int); i++)
    printf("%d ", nums[i]);
  printf("\n");
  return 0;
}
相关文章
|
C语言 C++
C语言 之 内存函数
C语言 之 内存函数
245 3
|
12月前
|
安全 C语言
C语言中的字符、字符串及内存操作函数详细讲解
通过这些函数的正确使用,可以有效管理字符串和内存操作,它们是C语言编程中不可或缺的工具。
508 15
|
存储 缓存 算法
【C语言】内存管理函数详细讲解
在C语言编程中,内存管理是至关重要的。动态内存分配函数允许程序在运行时请求和释放内存,这对于处理不确定大小的数据结构至关重要。以下是C语言内存管理函数的详细讲解,包括每个函数的功能、标准格式、示例代码、代码解释及其输出。
644 6
|
程序员 C++ 容器
在 C++中,realloc 函数返回 NULL 时,需要手动释放原来的内存吗?
在 C++ 中,当 realloc 函数返回 NULL 时,表示内存重新分配失败,但原内存块仍然有效,因此需要手动释放原来的内存,以避免内存泄漏。
|
存储 C语言
【c语言】字符串函数和内存函数
本文介绍了C语言中常用的字符串函数和内存函数,包括`strlen`、`strcpy`、`strcat`、`strcmp`、`strstr`、`strncpy`、`strncat`、`strncmp`、`strtok`、`memcpy`、`memmove`和`memset`等函数的使用方法及模拟实现。文章详细讲解了每个函数的功能、参数、返回值,并提供了具体的代码示例,帮助读者更好地理解和掌握这些函数的应用。
390 0
|
C语言 C++
c语言回顾-内存操作函数
c语言回顾-内存操作函数
|
12月前
|
存储
阿里云轻量应用服务器收费标准价格表:200Mbps带宽、CPU内存及存储配置详解
阿里云香港轻量应用服务器,200Mbps带宽,免备案,支持多IP及国际线路,月租25元起,年付享8.5折优惠,适用于网站、应用等多种场景。
3369 0
|
12月前
|
存储 缓存 NoSQL
内存管理基础:数据结构的存储方式
数据结构在内存中的存储方式主要包括连续存储、链式存储、索引存储和散列存储。连续存储如数组,数据元素按顺序连续存放,访问速度快但扩展性差;链式存储如链表,通过指针连接分散的节点,便于插入删除但访问效率低;索引存储通过索引表提高查找效率,常用于数据库系统;散列存储如哈希表,通过哈希函数实现快速存取,但需处理冲突。不同场景下应根据访问模式、数据规模和操作频率选择合适的存储结构,甚至结合多种方式以达到最优性能。掌握这些存储机制是构建高效程序和理解高级数据结构的基础。
1104 1