当前位置:网站首页>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!
边栏推荐
- 编程语言:类型系统的本质
- Output student grades
- Tonybot humanoid robot starts for the first time 0630
- Zzuli:1049 square sum and cubic sum
- String substitution
- 【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
- Tonybot Humanoïde Robot Infrared Remote play 0630
- Zzuli:1045 numerical statistics
- Luogu p4047 [jsoi2010] tribal division solution
- adc128s022 ADC verilog设计实现
猜你喜欢

tonybot 人形机器人 首次开机 0630

NPM install is stuck with various strange errors of node NPY

Sub GHz wireless solution Z-Wave 800 Series zg23 SOC and zgm230s modules

Sword finger offer 28 Symmetric binary tree

tonybot 人形机器人 红外遥控玩法 0630

tonybot 人形机器人 查看端口并对应端口 0701

如何查询淘宝天猫的宝贝类目

tonybot 人形機器人 紅外遙控玩法 0630

tonybot 人形机器人 定距移动 代码编写玩法

Pyqt interface production (login + jump page)
随机推荐
Zzuli:1042 sum of sequence 3
X86 assembly language - Notes from real mode to protected mode
7-3 count the number of words in a line of text
Solr series of full-text search engines - basic principles of full-text search
分布式事务(Seata) 四大模式详解
Find the sum of the elements of each row of the matrix
Bibit pharmaceutical rushed to the scientific innovation board: annual revenue of 970000, loss of 137million, proposed to raise 2billion
Detailed explanation of four modes of distributed transaction (Seata)
7-2 and then what time (15 minutes)
Zzuli: sum of 1051 square roots
How to query the baby category of tmall on Taobao
Zzuli:1048 factorial table
Tonybot humanoid robot infrared remote control play 0630
7-28 monkeys choose King (Joseph problem)
Ultra simple mobile map development
7-18 finding the single root of polynomial by dichotomy
556. The next larger element III: simple construction simulation questions
ConstraintLayout 的使用
天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库
【7.3】146. LRU缓存机制