一文带你玩转内存操作函数

简介: 一文带你玩转内存操作函数

目录

memcpy

memcpy的介绍和使用

memcpy的模拟实现

memmove

memmove的介绍和使用

memmove的模拟实现

memcmp

memcmp的介绍和使用

memset

memset的介绍和使用


memcpy

void * memcpy ( void * destination, const void * source, size_t num )

memcpy的介绍和使用

Copies the values of num bytes from the location pointed to by source directly to the memory block pointed to by destination.The underlying type of the objects pointed to by both the source and destination pointers are irrelevant for this function; The result is a binary copy of the data.The function does not check for any terminating null character in source - it always copies exactly num bytes.To avoid overflows, the size of the arrays pointed to by both the destination and source parameters, shall be at least num bytes, and should not overlap (for overlapping memory blocks, memmove is a safer approach).

函数memcpy从source这个位置开始向后复制num个字节的数据到destination的内存位置

这个函数在遇到'\0'的时候并不会停止

如果source和destination有任何的重叠,复制的结果都是未定义的

解释:这里的函数参数之所以是void*,是为了适用于多种类型,可以接受多种类型的参数。且不能将一个数组的地址作为两个参数传给memcpy,不然复制的结果就是乱糟糟的,不可预测。

//memcpy的使用
#include <stdio.h>
#include <string.h>
int main()
{
  int arr1[] = { 2, 3, 4, 5, 6, 7, 8, 9 };
  int arr2[] = { 1,1,1,1,1 };
  memcpy(arr1, arr2, 20);
  for (int i = 0; i < 8; i++)
  {
    printf("%d ", arr1[i]);
  }
  return 0;
}

memcpy的模拟实现

//memcpy的模拟实现
#include <stdio.h>
#include <string.h>
#include <assert.h>
void* my_memcpy(void* dest, const void* source, size_t num)
{
  void* ret = dest;
  assert(dest && source);
  while (num--)
  {
    *(char*)dest = *(char*)source;
    dest = (char*)dest + 1;
    source = (char*)source + 1;
  }
  return ret;
}
int main()
{
  int arr1[] = { 2, 3, 4, 5, 6, 7, 8, 9 };
  int arr2[] = { 1,1,1,1,1 };
  my_memcpy(arr1, arr2, 20);
  for (int i = 0; i < 8; i++)
  {
    printf("%d ", arr1[i]);
  }
  return 0;
}

memmove

void * memmove ( void * destination, const void * source, size_t num )

memmove的介绍和使用

Copies the values of num bytes from the location pointed by source to the memory block pointed by destination. Copying takes place as if an intermediate buffer were used, allowing the destination and source to overlap.The underlying type of the objects pointed by both the source and destination pointers are irrelevant for this function; The result is a binary copy of the data.The function does not check for any terminating null character in source - it always copies exactly num bytes.To avoid overflows, the size of the arrays pointed by both the destination and source parameters, shall be at least num bytes.

memmove是memcpy的优化,和memcpy的区别就是memmove函数处理的原内存块和目标内存块可以重叠

如果源空间和目标空间出现重叠,就得用memmove函数处理

//memmove的使用
#include <stdio.h>
#include <string.h>
int main()
{
  int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
  memmove(arr1, arr1 + 3, 20);
  for (int i = 0; i < 10; i++)
  {
    printf("%d ", arr1[i]);
  }
  return 0;
}

memmove的模拟实现

//memmove的模拟实现
#include <stdio.h>
#include <string.h>
#include <assert.h>
void* my_memmove(void* dest, const void* source, size_t num)
{
  void* ret = dest;
  assert(dest && source);
  while (num--)
  {
    if (dest < source) //前->后
    {
      *(char*)dest = *(char*)source;
      dest = (char*)dest + 1;
      source = (char*)source + 1;
    }
    else if (dest > source) //后->前
    {
      *((char*)dest + num) = *((char*)source + num);
    }
  }
  return ret;
}
int main()
{
  int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
  my_memmove(arr1, arr1 + 3, 20);
  for (int i = 0; i < 10; i++)
  {
    printf("%d ", arr1[i]);
  }
  return 0;
}

memcmp

int memcmp ( const void * ptr1, const void * ptr2, size_t num )

memcmp的介绍和使用

compares the first num bytes of the block of memory pointed by ptr1 to the first num bytes pointed by ptr2, returning zero if they all match or a value different from zero representing which is greater if they do not.Notice that, unlike strcmp, the function does not stop comparing after finding a null character.

比较它们从prt1和prt2指针开始的num个字节,相同返回0,prt1<prt2返回负数,prt1>prt2返回正数

//memcmp的使用
#include <stdio.h>
#include <string.h>
int main()
{
  char arr1[] = {1,2,3,4,5,6,7};
  char arr2[] = {1,2,3,4,2};
  int ret = memcmp(arr1, arr2, 20);
  printf("%d\n", ret);
  return 0;
}

memset

void * memset ( void * ptr, int value, size_t num )

memset的介绍和使用

Sets the first num bytes of the block of memory pointed by ptr to the specified value (interpreted as an unsigned char)

从ptr指向的内存块开始向后num个字节,改为value的内容

//memset的使用
#include <stdio.h>
#include <string.h>
int main()
{
  char arr[] = "hello world";
  memset(arr, '0', 3);
  printf("%s\n", arr);
  return 0;
}


目录
相关文章
|
1月前
|
程序员 C语言
C语言库函数 — 内存函数(含模拟实现内存函数)
C语言库函数 — 内存函数(含模拟实现内存函数)
33 0
|
2月前
|
编译器 C语言 C++
【C语言】realloc()函数详解(动态内存开辟函数)
【C语言】realloc()函数详解(动态内存开辟函数)
30 0
|
2月前
|
编译器 C语言 C++
【C语言】malloc()函数详解(动态内存开辟函数)
【C语言】malloc()函数详解(动态内存开辟函数)
56 2
|
2月前
|
编译器 C语言 C++
【C语言】memset()函数(内存块初始化函数)
【C语言】memset()函数(内存块初始化函数)
27 0
|
2月前
|
编译器 C语言 C++
【C语言】memcpy()函数(内存块拷贝函数)
【C语言】memcpy()函数(内存块拷贝函数)
45 0
|
17天前
|
C语言
C语言:内存函数(memcpy memmove memset memcmp使用)
C语言:内存函数(memcpy memmove memset memcmp使用)
|
19天前
|
编译器 C语言
字符串与内存函数
字符串与内存函数
25 0
|
2天前
|
存储 编译器 C语言
C语言:字符函数 & 字符串函数 & 内存函数
C语言:字符函数 & 字符串函数 & 内存函数
11 2
|
5天前
|
编译器
练习使用动态内存相关的4个函数:malloc、calloc、realloc、free
在了解使用动态内存相关的四个函数之前,我们先了解一下,为什么要有动态内存分配?
14 0