C语言内存函数

文章目录

1.memcpy使用和模拟实现

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

  • memcpy的使⽤需要包含<string.h>
  • memcpy是完成内存块拷⻉的,不关注内存中存放的数据是啥
  • 如果source和destination有任何的重叠,复制的结果都是未定义的。(内存重叠的情况使⽤memmove 就⾏)
  • 函数memcpy从source 的位置开始向后复制num 个字节的数据到destination 指向的内存
    代码使用
c 复制代码
#include <stdio.h>
int main()
{
   int arr1[20] = { 0 };
   int arr2[] = { 1,2,3,4,5 };
   memcpy(arr1, arr2, 20);
   for(int i=0;i < 5;i++)
   {
   	printf("%d ", arr2[i]);
   }
   return 0;
}

模拟实现

c 复制代码
void* my_memcpy(void* dest, const void* source,size_t count)
{
	void* ret = dest;
	assert(dest && source);
	while (count--)
	{
		*(char*)dest = *(char*)source;
		dest = (char*)dest + 1;
		source = (char*)source + 1;
	}
	return ret;
}

2.memmove的使用和模拟实现

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

  • memmove函数也是完成内存块拷⻉的
  • 和memcpy的差别就是memmove函数处理的源内存块和⽬标内存块是可以重叠的。
  • memmove的使⽤需要包含<string.h>

使用

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

结果: 1 2 1 2 3 4 5 8 9 10

3.memset函数的实现

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

  • memset 的使⽤需要包含参数:<string.h>
c 复制代码
#include <stdio.h>
#include <string.h>
int main()
{
	char str[] = "hello world";
	memset(str, 'x', 6);
	printf("%s", str);
	return 0;
}

结果:xxxxxxworld

模拟实现

c 复制代码
void* memmove(void* dst, const void* src, size_t count)
{
    void* ret = dst;
    if (dst <= src || (char*)dst >= ((char*)src + count))
    {
        while (count--) 
        {
            *(char*)dst = *(char*)src;
            dst = (char*)dst + 1;
            src = (char*)src + 1;
        }
    }
    else {
      
        dst = (char*)dst + count - 1;
        src = (char*)src + count - 1;
        while (count--) {
            *(char*)dst = *(char*)src;
            dst = (char*)dst - 1;
            src = (char*)src - 1;
        }
    }
    return(ret);
}

当有⼀块内存空间需要设置内容的时候,就可以使⽤memset函数,值得注意的是memset函数对内存
单元的设置是以字节为单位的

4.memcmp函数的使用

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

  • memcmp 的使⽤需要包含<string.h>
c 复制代码
#include <stdio.h>
#include <string.h>
int main()
{
	char buffer1[] = "DWgaOtP";
	char buffer2[] = "DWGAOTP";
	int n;
	n = memcmp(buffer1, buffer2, sizeof(buffer1));
	if (n > 0)
		printf("⼤于\n");
	else if (n < 0)
		printf("⼩于\n");
	else
		printf("⼀样\n");
	return 0;
}

如果要⽐较2块内存单元的数据的⼤⼩,可以用memcmp 函数,这个函数的特点就是可以指定⽐较⻓度。memcmp 函数是通过返回值告知⼤⼩关系的。

相关推荐
Camellia03113 分钟前
嵌入式学习--江协51单片机day6
嵌入式硬件·学习·51单片机
米粉03053 分钟前
算法图表总结:查找、排序与递归(含 Mermaid 图示)
数据结构·算法·排序算法
人类发明了工具22 分钟前
【优化算法】协方差矩阵自适应进化策略(Covariance Matrix Adaptation Evolution Strategy,CMA-ES)
线性代数·算法·矩阵·cma-es
黑色的山岗在沉睡25 分钟前
LeetCode100.4 移动零
数据结构·算法·leetcode
霖0027 分钟前
PCIe数据采集系统
数据结构·经验分享·单片机·嵌入式硬件·fpga开发·信号处理
xueyinan31 分钟前
小刚说C语言刷题—1088求两个数M和N的最大公约数
c语言
ClearViper333 分钟前
Java的多线程笔记
java·开发语言·笔记
敷啊敷衍33 分钟前
深入探索 C++ 中的 string 类:从基础到实践
开发语言·数据结构·c++
学地理的小胖砸43 分钟前
【Python 面向对象】
开发语言·python
方博士AI机器人44 分钟前
算法与数据结构 - 二叉树结构入门
数据结构·算法·二叉树