当前位置:网站首页>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.
边栏推荐
- Don't know about SynchronousQueue?So ArrayBlockingQueue and LinkedBlockingQueue don't and don't know?
- IO stream basics
- 链上治理为何如此重要,波卡Gov 2.0又会如何引领链上治理的发展?
- 接地气讲解TCP协议和网络程序设计
- 07-SDRAM :FIFO控制模块
- Quick solution for infix to suffix and prefix expressions
- An interesting project--Folder comparison tool (1)
- Using the "stack" fast computing -- reverse polish expression
- Arduino Basic Syntax
- 22.支持向量机—高斯核函数
猜你喜欢
【HCIP】BGP小型实验(联邦,优化)
How to find new potential projects?Tools recommended
PHP从txt文件中读取数据的方法
[Headline] Written test questions - minimum stack
不要用jOOQ串联字符串
一篇永久摆脱Mysql时区错误问题,idea数据库可视化插件配置
Axure tutorial - the new base (small white strongly recommended!!!)
认识USB、Type-C、闪电、雷电接口
解析正则表达式的底层实现原理
[21-Day Learning Challenge] A small summary of sequential search and binary search
随机推荐
poker question
基于数据驱动的变电站巡检机器人自抗扰控制
bgp aggregation reflector federation experiment
【无标题】
基于相关性变量筛选偏最小二乘回归的多维相关时间序列建模方法
具有通信时延的多自主体系统时变参考输入的平均一致性跟踪
460. LFU 缓存
go语言标准库fmt包怎么使用
【21天学习挑战赛】顺序查找和二分查找的小总结
链上治理为何如此重要,波卡Gov 2.0又会如何引领链上治理的发展?
一篇永久摆脱Mysql时区错误问题,idea数据库可视化插件配置
c语言字符和字符串函数总结(二)
【HCIP】BGP小型实验(联邦,优化)
ROS dynamic parameters
短视频SEO搜索运营获客系统功能介绍
Arduino 基础语法
IP核:FIFO
路由策略
Unknown CMake command “add_action_files“
测试点等同于测试用例吗