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 函数是通过返回值告知⼤⼩关系的。

相关推荐
uyeonashi几秒前
【QT控件】输入类控件详解
开发语言·c++·qt
飞川撸码1 分钟前
【LeetCode 热题100】网格路径类 DP 系列题:不同路径 & 最小路径和(力扣62 / 64 )(Go语言版)
算法·leetcode·golang·动态规划
Neil今天也要学习6 分钟前
永磁同步电机参数辨识算法--IPMSM拓展卡尔曼滤波全参数辨识
单片机·嵌入式硬件·算法
yzx9910131 小时前
基于 Q-Learning 算法和 CNN 的强化学习实现方案
人工智能·算法·cnn
iCxhust1 小时前
Prj10--8088单板机C语言8259测试(1)
c语言·开发语言
亮亮爱刷题1 小时前
算法练习-回溯
算法
眼镜哥(with glasses)2 小时前
蓝桥杯 国赛2024python(b组)题目(1-3)
数据结构·算法·蓝桥杯
крон4 小时前
【Auto.js例程】华为备忘录导出到其他手机
开发语言·javascript·智能手机
zh_xuan5 小时前
c++ 单例模式
开发语言·c++·单例模式
老胖闲聊5 小时前
Python Copilot【代码辅助工具】 简介
开发语言·python·copilot