当前位置:网站首页>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.
边栏推荐
- Async/await principle and execution sequence analysis
- [HCIP] BGP Small Experiment (Federation, Optimization)
- 以交易为生是一种什么体验?
- 使用jOOQ将Oracle风格的隐式连接自动转换为ANSI JOIN
- 【无标题】
- 22.支持向量机—高斯核函数
- 面试高频考题解法——栈的压入弹出序列、有效的括号、逆波兰表达式求值
- Don't know about SynchronousQueue?So ArrayBlockingQueue and LinkedBlockingQueue don't and don't know?
- 如何设计循环队列?快进来学习~
- 利用“栈”快速计算——逆波兰表达式
猜你喜欢
随机推荐
An interesting project--Folder comparison tool (1)
QML package management
REST会消失吗?事件驱动架构如何搭建?
Cyber-Physical System State Estimation and Sensor Attack Detection
632. Minimum interval
Interview high-frequency test questions solution - stack push and pop sequence, effective parentheses, reverse Polish expression evaluation
以交易为生是一种什么体验?
单片机遥控开关系统设计(结构原理、电路、程序)
【21天学习挑战赛】顺序查找和二分查找的小总结
Quick solution for infix to suffix and prefix expressions
什么是低代码(Low-Code)?低代码适用于哪些场景?
面试高频考题解法——栈的压入弹出序列、有效的括号、逆波兰表达式求值
Multidimensional Correlation Time Series Modeling Method Based on Screening Partial Least Squares Regression of Correlation Variables
【加密周报】经济衰退在加息气氛中蔓延 美联储“放手一搏”?盘点上周加密市场发生的重大事件
基于注意力机制的多特征融合人脸活体检测
146. LRU 缓存
短视频SEO搜索运营获客系统功能介绍
CRS 管理与维护
What is it like to trade for a living?
460. LFU cache






![[Headline] Written test questions - minimum stack](/img/67/08f2be8afc780e3848371a1b5e04db.png)


