详解C语⾔的mem系列函数
⽬录
printf函数是如何实现的
总结
memchr的函数声明:
void *memchr(const void *str, int c, size_t n);
作⽤:
memchr函数从str位置后的n个位置开始寻,寻第⼀个和c相同的字符。如果成功,memchr函数返回⼀个指向该字符位置的指针,如果没有没有到指定字符,则返回NULL。
实例:
#include <stdio.h>
#include <string.h>
int main()
{
const char* a = "my blog";
if (memchr(a, 'g', 7) != NULL)
printf("g is found\n");
if (memchr(a, 'g', 2) != NULL)
printf("g is found\n");
return 0;
}
函数的实现:
v oid* my_memchr(const char* str,int c,size_t n)
{
assert(str != NULL);
while ((n--) && (*(str++) - c));
if (!(*str - c))
return NULL;
return str;
}
注意事项:形参中的c是⼀个⽆符号字符。
函数的声明:
int memcmp( const void *buf1, const void *buf2, size_t count );
作⽤:
memcmp⽤于⽐较buf1 和 buf2 的前count个字节进⾏⽐较,如果buf1⼤于buf2,该函数返回⼀个正数,如果⼩于则返回⼀个负数,相等则返回0。
实例:
#include <stdio.h>
#include <string.h>
int main()
{
char str1[15];
char str2[15];
int ret;
memcpy(str1, "aBcDDDD", 8);
memcpy(str2, "aBCdddd", 8);
ret = memcmp(str1, str2, 7);
printf("%s ", str1);
if (ret > 0)
printf("⼤于");
else if (ret < 0)
printf("⼩于");
else
printf("等于");
printf(" %s\n", str2);
return 0;
}
函数的实现:
⾸先我们需要了解memcmp是怎么⽐较⼤⼩的。通过不断地调整上⾯实例的两个字符串,我发现,该函数是从第⼀个字节开始⽐较,如果相同,则继续⽐较下⼀个字节,如果有⼤⼩差异,则将这两个字节的⼤⼩差异作为结果输出。
int my_memcmp(const void* buf1,const void *buf2,size_t count)
{
assert(buf1 && buf2);
while ((count--) && !(*(((char*)buf1)++) - *(((char*)buf2)++)));
return *(--(char*)buf1) - *(--(char*)buf2);//这⾥要 -- 是因为上⾯的最后还++了⼀下
}
函数的声明:
void *memcpy( void *dest, const void *src, size_t count );
作⽤:
该函数将 src 的 count 个字节复制到 dest。该函数返回 dest 的起始位置。
实例:
#include <stdio.h>
#include <string.h>
int main()
{
char arr[50] = { 0 };
char* b = "csdn";
memcpy(arr, b, strlen(b));
printf("%s", arr);
return 0;
}
函数的实现:
void* my_memcpy(void* a, const void* b, size_t count)
{
assert(a && b);
void* ret = a;
while (count--)
{
*(char*)a = *(char*)b;
a = (char*)a + 1;
b = (char*)b + 1;
}
return ret;
}
函数声明:
void *memmove( void *dest, const void *src, size_t count );
作⽤:
该函数的作⽤和memcpy类似。但是为什么会有memmove呢?
我们看下⾯这段代码
#include <stdio.h>
#include <string.h>
int main()
{
int arr[10] = { 1,2,3,4,5,6,7,8,9,10 };
memcpy(arr + 3, arr, 7);
int i = 0;
for (i = 0; i < 10; i++)
printf("%d ", arr[i]);
return 0;
}
我们可能认为答案是 1 2 3 1 2 3 4 5 6 7
但是vs2022给出的结果是
这是因为,内存只有⼀块,可能会冲突。⽐如 4  这个位置,⼀开始 4 被修改为 1 ,再后来,当dest指针指到这个1的时候,他⼜会把这个1放到后⾯的位置。⽽memmove就能解决这个问题。
它是如何解决的呢?
对于刚才这种情况,我们发现 dest(红)⼤于 src(蓝),如果我们从左开始,往右开始修改,那么肯定会出现刚才那种情况,但是如果是从右往左呢?思考后我们发现,这是可⾏的,就好像右边的⼈在追左边的⼈,然后左边的⼈不断地往后丢东西,都丢在右边的⼈的⾝上。同时,我们发现,如果src(蓝)的最右端超过了dest(红)的最右端,这种⽅法好像⼜不适⽤了,也不能再从右往左了。
我们知道,dest和src的⼤⼩应该都是count,所以不存在上⾯这种情况。
那就可以开始具体实现了。
void* my_memmove(void* a,const void* b,size_t count) //a:dest b:source
{
assert(a && b);
void* ret = a;
//s<d 从右往左
if (b < a)
{
a = (char*)a + count - 1;
b = (char*)b + count - 1;
while (count--)
{
*(char*)a = *(char*)b;
a = (char*)a - 1;
b = (char*)b - 1;
}
}
else
{
while (count--)
{
*(char*)a = *(char*)b;
a = (char*)a + 1;
b = (char*)b + 1;
}
}
return ret;
}
函数声明:
void *memset( void *dest, int c, size_t count );
作⽤:就是可以初始化⼀块内存为具体值。
实例:
#include <stdio.h>
#include <string.h>
int main()
{
char p[20] = "what is csdn";
memset(p, '#', 4);
printf("%s", p);
return 0;
}
函数实现:
void* my_memset(void* dest, int c, size_t count)
{
void* tmp = dest;
while (count--)
*(((char*)dest)++) = (char)c;
return tmp;
}
总结
本篇⽂章就到这⾥了,希望能够给你带来帮助,也希望您能够多多关注的更多内容!