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

相关推荐
paterWang2 小时前
基于 Python 和 OpenCV 的酒店客房入侵检测系统设计与实现
开发语言·python·opencv
东方佑2 小时前
使用Python和OpenCV实现图像像素压缩与解压
开发语言·python·opencv
饮长安千年月2 小时前
Linksys WRT54G路由器溢出漏洞分析–运行环境修复
网络·物联网·学习·安全·机器学习
红花与香菇2____2 小时前
【学习笔记】Cadence电子设计全流程(二)原理图库的创建与设计(上)
笔记·嵌入式硬件·学习·pcb设计·cadence·pcb工艺
mit6.8242 小时前
[实现Rpc] 通信类抽象层 | function | using | 解耦合设计思想
c++·网络协议·rpc
我真不会起名字啊2 小时前
“深入浅出”系列之杂谈篇:(3)Qt5和Qt6该学哪个?
开发语言·qt
laimaxgg2 小时前
Qt常用控件之单选按钮QRadioButton
开发语言·c++·qt·ui·qt5
水瓶丫头站住3 小时前
Qt的QStackedWidget样式设置
开发语言·qt
尼尔森系4 小时前
排序与算法:希尔排序
c语言·算法·排序算法
小钊(求职中)4 小时前
Java开发实习面试笔试题(含答案)
java·开发语言·spring boot·spring·面试·tomcat·maven