当前位置:网站首页>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!
边栏推荐
- Use of form text box (I) select text
- 7-22 tortoise and rabbit race (result oriented)
- Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion
- Amazon, express, lazada, shopee, eBay, wish, Wal Mart, Alibaba international, meikeduo and other cross-border e-commerce platforms evaluate how Ziyang account can seize traffic by using products in th
- Zzuli:1049 square sum and cubic sum
- Zzuli:1054 monkeys eat peaches
- 适用于XP的DDK
- Some concepts about agile
- 洛谷P5536 【XR-3】核心城市 题解
- Zzuli:1048 factorial table
猜你喜欢

Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in

dllexport和dllimport

Pyqt interface production (login + jump page)

puzzle(016.4)多米诺效应

Dllexport and dllimport

7-15 calculation of PI

Analysis of gene family characteristics - chromosome location analysis

adc128s022 ADC verilog设计实现
![Luogu p4047 [jsoi2010] tribal division solution](/img/7f/3fab3e94abef3da1f5652db35361df.png)
Luogu p4047 [jsoi2010] tribal division solution

tonybot 人形机器人 首次开机 0630
随机推荐
分布式事务(Seata) 四大模式详解
一文了解微分段应用场景与实现机制
NPM install is stuck with various strange errors of node NPY
Statistical capital consonants
Optical cat super account password and broadband account password acquisition
7-16 find the set of integers that meet the given conditions
SSH访问控制,多次失败登录即封掉IP,防止暴力破解
Recent learning summary
Programming language: the essence of type system
Zzuli: sum of 1051 square roots
7-19 check denomination (solve binary linear equation)
7-10 stack of hats (25 points) (C language solution)
Get permissions dynamically
tonybot 人形机器人 查看端口并对应端口 0701
Why is this error reported when modifying records in the database
Zzuli:1048 factorial table
Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion
Thread.sleep和TimeUnit.SECONDS.sleep的区别
China PETG market forecast and Strategic Research Report (2022 Edition)
Understand the application scenario and implementation mechanism of differential segment