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

相关推荐
Sylvia-girl1 小时前
Java——抽象类
java·开发语言
Yana.nice3 小时前
Bash函数详解
开发语言·chrome·bash
西西西仓鼠4 小时前
python学习打卡:DAY 40 训练和测试的规范写法
学习
m0_535064604 小时前
C++模版编程:类模版与继承
java·jvm·c++
Magnetic_h4 小时前
【iOS】方法与消息底层分析
笔记·学习·macos·ios·objective-c·cocoa
今天背单词了吗9805 小时前
算法学习笔记:19.牛顿迭代法——从原理到实战,涵盖 LeetCode 与考研 408 例题
笔记·学习·算法·牛顿迭代法
没书读了5 小时前
考研复习-数据结构-第六章-图
数据结构
tomorrow.hello5 小时前
Java并发测试工具
java·开发语言·测试工具
晓13135 小时前
JavaScript加强篇——第四章 日期对象与DOM节点(基础)
开发语言·前端·javascript
老胖闲聊5 小时前
Python I/O 库【输入输出】全面详解
开发语言·python