当前位置:网站首页>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!
边栏推荐
- Dllexport et dllimport
- Creation of data table of Doris' learning notes
- Tonybot humanoid robot checks the port and corresponds to port 0701
- 添加Zabbix计算类型项目Calculated items
- Dllexport and dllimport
- Plane vector addition
- US stock listing of polar: how can the delivery of 55000 units support the valuation of more than 20billion US dollars
- Luogu p5194 [usaco05dec]scales s solution
- Programming language: the essence of type system
- 表单文本框的使用(一) 选择文本
猜你喜欢
Detailed explanation of four modes of distributed transaction (Seata)
洛谷P5018 [NOIP2018 普及组] 对称二叉树 题解
Tonybot Humanoïde Robot Infrared Remote play 0630
NPM install is stuck with various strange errors of node NPY
How to query the baby category of tmall on Taobao
tonybot 人形机器人 查看端口并对应端口 0701
7-15 calculation of PI
556. The next larger element III
Creation of data table of Doris' learning notes
Similarities and differences between Allegro, OrCAD, net alias, port, off page connector and how to select them
随机推荐
Zzuli:1040 sum of sequence 1
Analysis of gene family characteristics - chromosome location analysis
Luogu p5018 [noip2018 popularization group] symmetric binary tree problem solution
7-19 check denomination (solve binary linear equation)
String substitution
Sword finger offer 28 Symmetric binary tree
J-luggage lock of ICPC Shenyang station in 2021 regional games (simple code)
etcd集群权限管理和账号密码使用
Sendmail无法发送邮件及发送过慢解决
Etcd cluster permission management and account password usage
Dllexport et dllimport
puzzle(016.3)千丝万缕
Detailed explanation of four modes of distributed transaction (Seata)
Mysql多表查询 #子查询
7-6 mixed type data format input
Tonybot humanoid robot starts for the first time 0630
7-14 sum integer segments (C language)
Sendmail can't send mail and it's too slow to send. Solve it
亚马逊、速卖通、Lazada、Shopee、eBay、wish、沃尔玛、阿里国际、美客多等跨境电商平台,测评自养号该如何利用产品上新期抓住流量?
Time conversion ()