前言

        本文学习的为内存函数(memcpy函数,memmove函数,memset函数,memcmp函数),内存函数与上次讲到的字符串函数有相似之处,学习前可以先去了解了解上一讲的内容,上一讲链接:字符函数与字符串函数(C语言)-CSDN博客,希望本文学习会对你有所帮助,正文如下:

一,memcpy函数

        1,认识memcpy函数

        memcpy是C与C++ 标准库中的一个函数,用于将一段内存区域的内容复制到另一段内存区域,其头文件为<string.h>。

void* memcpy(void* dest, const void* src, size_t n);
·dest 为目标内存地址,需要复制到的位置。

·src 为源内存的地址,需要复制的数据。

·n 为需要复制的字节数。

注意:

        1,这个函数在遇到 '\0' 的时候并不会停下来。

        2,如果src和dest有任何的重叠,复制的结果都是未定义的。(此时应使用memmove)

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<assert.h>
#include<errno.h>


int main() 
{
	int a[] = { 1,2,3,4,5,6,7,8,9 };
	int b[20] = { 0 };
	memcpy(b, a, 8);//<int>类型是4个字节
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", b[i]);
	}

	printf("\n");
	char c[] = "1234567";
	char d[20];
	memcpy(d, c, 8);//<char>类型是1个字节
	printf("%s ", d);
	
	return 0;
}

        2,memcpy函数的模拟实现

        ·因为 memcpy 函数为void*类型,void*类型不可以继续++与--等操作,所以我们应该进行强制转换,并且因为此函数为一字节一字节进行拷贝,所以我们应该强制转换为 char* 类型进行操作;

        ·目标变量 dst 的地址需要存放在一个临时指针变量中,因为 dst 在循环体中一直是变化的;

        ·还需要对 dst 与 src 进行 assert 断言操作,防止 dst 与 src 为空;

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
#include<assert.h>
#include<math.h>

using namespace std;

void* my_memcpy(void* dst, const void* src, size_t count)
{
	void* ret = dst;
	assert(dst);
	assert(src);
	while (count--) {
		*(char*)dst = *(char*)src;
		dst = (char*)dst + 1;
		src = (char*)src + 1;
	}
	return(ret);
}

int main() {
	int a[] = { 1,2,3,4,5 };
	int b[20] = {0};
	my_memcpy(b, a, 16);
	for (int i = 0; i < 5; i++) {
		cout << b[i] << " ";
	}
	return 0;
}

        memcpy不支持自身内部拷贝,可能存在覆盖,主要负责拷贝两块独立空间中的数据,重叠内存拷贝使用 memmove

二,memmove函数的使用与模拟

        1,认识memmove函数

        memmove 是 C/C++ 标准库中的内存操作函数,用于将指定字节数的数据从源内存地址复制到目标内存地址。

        与 memcpy 不同,memmove 能正确处理内存重叠的情况,确保数据完整性。

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
#include<assert.h>
#include<math.h>

using namespace std;

int main() {
	int a[] = { 1,2,3,4,5 };
	int b[20] = {0};
	memmove(a, a + 2, 8);
	for (int i = 0; i < 5; i++) {
		cout << a[i] << " ";
	}
	return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
#include<assert.h>
#include<math.h>

using namespace std;

int main() {
	int a[] = { 1,2,3,4,5 };
	int b[20] = {0};
	memmove(a+2, a, 8);
	for (int i = 0; i < 5; i++) {
		cout << a[i] << " ";
	}
	return 0;
}

其他的与 memcpy 相同;

        2,memmove的模拟实现        

        memmove 函数与 memcpy 函数类似,都是进行内存复制的,我们已经学习了memcpy 函数了;唯一的不同是memmove 能正确处理内存重叠的情况,确保数据完整性,所以我们应该从这下手;

        memmove 实现内存重叠复制 需要考虑二种情况;

        1,dst 在 src 右边

        2,dst 在 src 左边

        为什么会有这二种情况,因为需要复制的值可能会因为复制而产生变化;

情况1:

        这种情况应该从后向前复制;结果如下:

        但是,如果没有从后向前复制,如下:

        会发现结果完全不同,并且需要复制的12345变为了12121;

情况2

        同理;

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
#include<assert.h>
#include<math.h>

using namespace std;

void* my_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);
}

int main() {
    int a[] = { 1,2,3,4,5,6,7,8,9 };
    my_memmove(a + 1, a, 4);
    for (int i = 0; i < 10; i++) {
        cout << a[i] << " ";
    }
    return 0;
}

三,memset函数的使用与模拟

        1,认识memset函数

    memset 是 C 标准库中的一个函数,用于将指定内存区域的前 num 个字节设置为特定的值 value。通常用于初始化内存块或填充缓冲区。

      

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
#include<assert.h>
#include<math.h>

using namespace std;

int main() {
	char a[] = "wxd666";
	memset(a, 'q', 3);
	cout << a;
	return 0;
}

        2,memset的模拟实现

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
#include<assert.h>
#include<math.h>

using namespace std;

void* my_memset(void* ptr, int value, size_t num) {
	assert(ptr);
	void* ret = ptr;
	while (num--) {
		*(char*)ptr = value;  
		ptr = (char*)ptr + 1; 
	}
	return ret;
}

int main() {
	char a[] = "wxd666";
	my_memset(a, 'q', 3);
	cout << a;
	return 0;
}

四,memcmp函数的使用与模拟

        1,认识memcmp函数

        memcmp 是 C/C++ 标准库中的函数,用于比较两块内存区域的内容是否相同。定义在 <string.h> 或 <cstring> 头文件中。

返回值表明
>0在两个内存块中不匹配的第一个字节在 ptr1 中的值大于在 ptr2 中的值
0两个内存块的内容相等
<0在两个内存块中不匹配的第一个字节在 ptr1 中的值小于在 ptr2 中的值
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
#include<assert.h>
#include<math.h>

using namespace std;

int main() {
	int a[] = { 1,2,3,4 };
	int b[] = { 1,3,3,4 };
	cout<<memcmp(a, b, 16);
	
	return 0;
}

        注意:

        1,逐字节比较,不考虑数据类型或字符编码

        2,strcmp 比较以 \0 结尾的字符串,memcmp 比较任意内存块(包括 \0)

        2,memcmp的模拟实现

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
#include<stdio.h>
#include<assert.h>
#include<math.h>

using namespace std;

int my_memcmp(const void* ptr1, const void* ptr2, size_t num) {
	assert(ptr1 && ptr2);
	while (num-- && *(char*)ptr1 == *(char*)ptr2) {
		ptr1 = (char*)ptr1 + 1;
		ptr2 = (char*)ptr2 + 1;
	}
	return(*(char*)ptr1 - *(char*)ptr2);
}

int main() {
	int a[] = { 1,2,3,4 };
	int b[] = { 1,1,3,4 };
	cout<<my_memcmp(a, b, 16);
	
	return 0;
}

结语:

        内存函数就已经讲完了,希望可以对你们有所帮助;

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐