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

相关推荐
FirstFrost --sy24 分钟前
数据结构之二叉树
c语言·数据结构·c++·算法·链表·深度优先·广度优先
Chef_Chen27 分钟前
从0开始学习计算机视觉--Day08--卷积神经网络
学习·计算机视觉·cnn
weixin_4462608527 分钟前
Isaac Lab:让机器人学习更简单的开源框架
学习·机器人
森焱森34 分钟前
垂起固定翼无人机介绍
c语言·单片机·算法·架构·无人机
搂鱼1145141 小时前
(倍增)洛谷 P1613 跑路/P4155 国旗计划
算法
Yingye Zhu(HPXXZYY)1 小时前
Codeforces 2021 C Those Who Are With Us
数据结构·c++·算法
liulilittle2 小时前
LinkedList 链表数据结构实现 (OPENPPP2)
开发语言·数据结构·c++·链表
我真不会起名字啊2 小时前
OpenSceneGraph(OSG)开发学习
学习
永日456702 小时前
学习日记-spring-day42-7.7
java·学习·spring
无聊的小坏坏2 小时前
三种方法详解最长回文子串问题
c++·算法·回文串