当前位置:网站首页>On MEM series functions of C language
On MEM series functions of C language
2022-07-03 14:36:00 【roseisbule】
C Of language mem Functions are used very frequently . Here is what I learned about myself mem Make a small summary of the series of functions .
1.memchr
memchr Function declaration of :
void *memchr(const void *str, int c, size_t n);
effect :
memchr Function from str After the position n Start looking for a place , Find the first and c The same characters . If it works ,memchr Function returns a pointer to the position of the character , If the specified character is not found , Then return to NULL.
example :
#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;
}
Implementation of function :
void* my_memchr(const char* str,int c,size_t n)
{
assert(str != NULL);
while ((n--) && (*(str++) - c));
if (!(*str - c))
return NULL;
return str;
}
matters needing attention : In formal parameter c Is an unsigned character .
2.memcmp
Declaration of functions :
int memcmp( const void *buf1, const void *buf2, size_t count );
effect :
memcmp For comparison buf1 and buf2 Before count Compare bytes , If buf1 Greater than buf2, This function returns a positive number , If it is less than, it returns a negative number , Equal returns 0.
example :
#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(" Greater than ");
else if (ret < 0)
printf(" Less than ");
else
printf(" be equal to ");
printf(" %s\n", str2);
return 0;
}
Implementation of function :
First we need to understand memcmp How to compare sizes . By constantly adjusting the two strings of the above example , I find , This function compares from the first byte , If the same , Then continue to compare the next byte , If there is a size difference , Then the size difference between these two bytes is output as the result .
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);// Here -- Because the last one on the top still ++ For a moment
}
3.memcpy
Declaration of functions :
void *memcpy( void *dest, const void *src, size_t count );
effect :
This function will src Of count Copy bytes to dest. This function returns dest Starting position .
example :
#include <stdio.h>
#include <string.h>
int main()
{
char arr[50] = { 0 };
char* b = "csdn.com";
memcpy(arr, b, strlen(b));
printf("%s", arr);
return 0;
}
Implementation of function :
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;
}
4.memmove
Function declaration :
void *memmove( void *dest, const void *src, size_t count );
effect :
The function is similar to memcpy similar . But why memmove Well ?
Let's look at the following code
#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;
}
We may think the answer is 1 2 3 1 2 3 4 5 6 7
however vs2022 The result is
This is because , There is only one piece of memory , There may be conflict . such as 4 This position , In limine 4 Changed to 1 , later , When dest The pointer points to this 1 When , He will put this again 1 Put it in the back . and memmove We can solve this problem .
How is it solved ?
For the situation just now , We found that dest( red ) Greater than src( blue ), If we start on the left , Start to modify on the right , Then there will definitely be the situation just now , But what if it's from right to left ? After thinking, we found , This is feasible , It's like the person on the right is chasing the person on the left , Then the person on the left kept throwing things back , All on the person on the right . meanwhile , We found that , If src( blue ) The rightmost end of exceeds dest( red ) Right most end of , This method seems to be inapplicable again , You can't go from right to left anymore .
We know ,dest and src The size of should be count, So there is no such situation .
Then you can start to implement .
void* my_memmove(void* a,const void* b,size_t count) //a:dest b:source
{
assert(a && b);
void* ret = a;
//s<d From right to left
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;
}
5.memset
Function declaration :
void *memset( void *dest, int c, size_t count );
effect : That is, you can initialize a piece of memory to a specific value .
example :
#include <stdio.h>
#include <string.h>
int main()
{
char p[20] = "what is csdn";
memset(p, '#', 4);
printf("%s", p);
return 0;
}
Function implementation :
void* my_memset(void* dest, int c, size_t count)
{
void* tmp = dest;
while (count--)
*(((char*)dest)++) = (char)c;
return tmp;
}
Bye!
边栏推荐
猜你喜欢
puzzle(016.3)千丝万缕
Use of form text box (I) select text
tonybot 人形机器人 定距移动 代码编写玩法
Zhonggan micro sprint technology innovation board: annual revenue of 240million, net loss of 17.82 million, proposed to raise 600million
dllexport和dllimport
X86 assembly language - Notes from real mode to protected mode
[qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?
556. The next larger element III
dllexport和dllimport
Tonybot humanoid robot infrared remote control play 0630
随机推荐
[qingniaochangping campus of Peking University] in the Internet industry, which positions are more popular as they get older?
论文分享:Generating Playful Palettes from Images
Zzuli:1048 factorial table
Use of form text box (I) select text
Zzuli:1045 numerical statistics
Zzuli:1052 sum of sequence 4
【7.3】146. LRU缓存机制
Some concepts about agile
洛谷P5536 【XR-3】核心城市 题解
ShowMeBug入驻腾讯会议,开启专业级技术面试时代
Showmebug entered Tencent conference, opening the era of professional technical interview
7-16 find the set of integers that meet the given conditions
556. 下一个更大元素 III : 简单构造模拟题
Mongodb index
retrofit
Although not necessarily the best, it must be the hardest!
Facebook 如何将 Instagram 从 AWS 搬到自己的服务器
J-luggage lock of ICPC Shenyang station in 2021 regional games (simple code)
7-1 positive integer a+b (15 points)
添加Zabbix计算类型项目Calculated items