当前位置:网站首页>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.
边栏推荐
- Mean Consistency Tracking of Time-Varying Reference Inputs for Multi-Agent Systems with Communication Delays
- IP核:FIFO
- 一文概览最实用的 DeFi 工具
- Axure tutorial - the new base (small white strongly recommended!!!)
- 146. LRU 缓存
- 基于超参数自动寻优的工控网络入侵检测
- What is the function of the JSP Taglib directive?
- Async/await principle and execution sequence analysis
- Unity—四元数、欧拉角API+坐标系统
- [21-Day Learning Challenge] A small summary of sequential search and binary search
猜你喜欢
随机推荐
uni-app项目总结
信息物理系统状态估计与传感器攻击检测
CRS 管理与维护
Redis的集群模式
图解LeetCode——1161. 最大层内元素和(难度:中等)
Using the "stack" fast computing -- reverse polish expression
Arduino Basic Syntax
Study Notes: The Return of Machine Learning
JSP如何使用page指令让JSP文件支持中文编码呢?
不要用jOOQ串联字符串
How to design a circular queue?Come and learn~
短视频seo搜索优化主要内容
使用jOOQ将Oracle风格的隐式连接自动转换为ANSI JOIN
面试:简单介绍你参与的一个项目
不了解SynchronousQueue?那ArrayBlockingQueue和LinkedBlockingQueue不会也不知道吧?
什么是低代码(Low-Code)?低代码适用于哪些场景?
Pytorch seq2seq 模型架构实现英译法任务
poker question
Ansible中的任务执行控制
632. 最小区间








