当前位置:网站首页>Identify memory functions memset, memcmp, memmove, and memcpy
Identify memory functions memset, memcmp, memmove, and memcpy
2022-08-02 00:32:00 【BSP junior XueSeng】
Blog homepage: https://blog.csdn.net/weixin_46094737?type=blog
Comments are welcome If there are any mistakes, please correct me!
This article was originally written by the primary school student Lian, first published on CSDN
The future is very long, and it is worth our efforts to strive for a better life!
1, memset is an initialization function, the function is to put a certain block Memory is all set to the specified value.
char a[5];memset(a,'a',5);puts(a);- a points to the block of memory to fill.
- 'a' is the value to be set.
- 5 is the number of characters to be set to this value.
Run result:

Notes:
The memset function initializes the memory block according to bytes, so it cannot be used to initialize the int array to a value other than 0 and -1 (unless the value is high byte and lowbytes are the same).
In fact, the actual range of c should be 0~255, because the memset function can only take the last eight bits of c for each byte of the input range.That is to say no matter how big c is, only the last eight binary digits are valid.
The memset function is very convenient for initialization processing, but it also has its limitations, such as pay attention to the initialization value, pay attention to the number of bytes and so on.Of course, it is also possible to directly choose to use for loop or while loop to initialize, but memset is faster.
2, memcmp compares the first count bytes of the memory areas buf1 and buf2. This function compares by bytes.
Basic prototype: int memcmp(const void *buf1, const void *buf2, unsigned int count);
Main function: Compare the first count bytes of memory areas buf1 and buf2.
Return value:
When buf1 When buf1=buf2, return value=0 When buf1>buf2, return value>0 Running result: ret=1 Note: This function compares by bytes. Example: When s1,s2 are strings, memcmp(s1,s2,1) is to compare the ascII code value of the first byte of s1 and s2; memcmp(s1,s2,n) is to compare the ascII code values of the first n bytes of s1 and s2, such as: char *s1="abc"; char *s2="acd"; int r=memcmp(s1,s2,3); It is to compare the first 3 bytes of s1 and s2, the first byte is equal, the size of the second byte comparison has been determined, and there is no need to continue to compare the third byte, so r=-1. memmove memory copy function, the function is to copy n bytes to the target address, the target memory and the source address memory can overlap. Another memory copy function, memcpy, cannot overlap memory. Run result: memcpy function is a function in C language forMemory copy function, declared in string.h.Its prototype is: Run result: p[0]=h p[1]=e p[2]=l p[3]=l p[4]=o p[5]=, p[6]=n p[7]=i p[8]=, p[9]=h p[10]=a p[11]=o The function is: take the address pointed to by source as the starting point, and copy consecutive n bytes of data to the address pointed to by destin as the starting point Memory. When using the memcpy function, you need to pay attention: When using the memcpy function, pay special attention to the data length.If the copied data type is char, then the data length is equal to the number of elements.And if the data type is other (such as int, double, custom structure, etc.), pay special attention to the value of the data length.//a[0] 10 00 00 00//a[1] 20 00 00 00int a[2]={10,20};int b[2]={10,11};//a[0] 10 00 00 00//a[1] 11 00 00 00int ret=-1;ret=memcmp(a,b,5);//Memory comparison function, the same is 0, the difference is 1 (byte!!!)printf("ret=%d",ret);3, memmove memory copy function
memmove is not a safe function, so pay attention to the generation of out-of-bounds when copying.An array of length n, using memmove to move backward one bit, can copy at most n-2 bytes. int a[10]={11,12,13,14,15,16,17,18,19,20};memmove(a,a+5,12);puts(a);
4, memcpy memory copy function
Principle:
void test_01(void)//Test string, but memcpy copy is still copied one character by one{char str01[]="hello,ni,hao";char str02[20];memcpy(str02,str01,12);print_c(str02,12);}
The function has three parameters, the first is the destination address, the second is the source address, and the third is the data length.The length of the data copied by the memcpy function
A good habit is to use n * sizeof(type_name) no matter what type of data is copied.
边栏推荐
猜你喜欢

Pytorch seq2seq 模型架构实现英译法任务

短视频SEO搜索运营获客系统功能介绍

An interesting project--Folder comparison tool (1)

Short video SEO search operation customer acquisition system function introduction

一篇永久摆脱Mysql时区错误问题,idea数据库可视化插件配置

鲲鹏编译调试插件实战

辨析内存函数memset、memcmp、memmove以及memcpy

图解LeetCode——1161. 最大层内元素和(难度:中等)

扑克牌问题

22. The support vector machine (SVM), gaussian kernel function
随机推荐
中缀转后缀、前缀表达式快速解决办法
Difference between JSP out.print() and out.write() methods
Graphical LeetCode - 1161. Maximum Sum of In-Layer Elements (Difficulty: Moderate)
JSP如何使用page指令让JSP文件支持中文编码呢?
How does JSP use the page command to make the JSP file support Chinese encoding?
TCL: Pin Constraints Using the tcl Scripting Language in Quartus
07-SDRAM :FIFO控制模块
基于相关性变量筛选偏最小二乘回归的多维相关时间序列建模方法
460. LFU 缓存
08-SDRAM: Summary
含外部储能的电力系统暂态稳定分布式控制
GIF making - very simple one-click animation tool
uni-app项目总结
[头条]笔试题——最小栈
JSP built-in object out object function introduction
JSP Taglib指令具有什么功能呢?
PHP从txt文件中读取数据的方法
els 方块边界变形处理
Cyber-Physical System State Estimation and Sensor Attack Detection
Keepalived 高可用的三种路由方案