当前位置:网站首页>Memory memory operation function
Memory memory operation function
2022-07-25 06:07:00 【N-order magic cube】
One 、malloc/calloc
name : | malloc/calloc |
function : | Dynamic memory allocation function |
The header file : | #include <memory.h> |
Function primitives : | void *malloc(size_t size); void *calloc(size_t num,size_t size); |
Parameters : | size Size of allocated memory block num Number of allocated memory blocks |
Return value : | The first address of the allocated memory block is returned successfully , Failure to return NULL. |
malloc and calloc Can allocate memory areas , but malloc Only one memory area can be requested at a time ,calloc You can apply for multiple memory areas at a time . in addition calloc The allocated memory area will be initially converted into 0,malloc No initialization .
#include <stdio.h>
#include <stdlib.h>
main()
{
int *p=NULL;
p=(int *)malloc(sizeof(int));
if(p==NULL)
{
printf("malloc error\n");
exit(1);
}
*p=3;
printf("%d\n",*p);
free(p);
}Two 、free
name : | free |
function : | Dynamic memory release function |
The header file : | #include <memory.h> |
Function primitives : | void free(void *ptr); |
Parameters : | ptr Use malloc or calloc Wait for the memory pointer returned by the memory allocation function |
Return value : | nothing |
free Can be released by malloc or calloc Memory allocated by the memory allocation function . When the program is large , You may have to dynamically allocate memory multiple times during , If not released in time , The program will take up a lot of memory . it is to be noted that , If ptr The memory referred to has been released or the memory address is unknown , Then unexpected things may happen . If the parameter is NULL, be free It won't work .
3、 ... and 、memset
name : | memset |
function : | Initialize the specified memory space |
The header file : | #include <memory.h> |
Function primitives : | void *memset(void *buffer,int c,int count); |
Parameters : | buffer Allocated memory c Initialize content count Number of bytes initialized |
Return value : | Return to point buffer The pointer to |
memset hold buffer Before the memory area referred to count Bytes set to a character ASCLL value . Generally used to give an array , String and other types of assignment .
main()
{
int *p=(int *)malloc(sizeof(int)*10); // Distribute 10 individual Int
if(p==NULL) exit(1); // Error detection
memset(p,0,sizeof(int)*10); // Memory block is set 0
free(p); // Release
}Four 、memcpy
name : | memcpy |
function : | Copy memory space |
The header file : | #include <memory.h> |
Function primitives : | void *memcpy(void *dest,void *src,unsigned int count); |
Parameters : | dest Target memory area src Original memory area count Bytes to copy |
Return value : | Point to dest The pointer to |
memcpy Will be able to src The memory area referred to is copied count Byte to dest The memory area referred to . If count Than src The number of bytes is large ,strcpy Can copy '\0' After that . it is to be noted that dest and src Don't overlap .memcpy Just copy memory space , Do not deal with spatial overlap .
main()
{
int *p1=malloc(sizeof(int)*10);
int *p2=malloc(sizeof(int)*5);
memset(p1,0,sizeof(int)*10);
memcpy(p2,p1,sizeof(int)*5);
free(p1);
free(p2);
)
5、 ... and 、memmove
name : | memmove |
function : | Copy ( Move ) Memory space |
The header file : | #include <memory.h> |
Function primitives : | void *memmove(void *dest,void *src,unsigned int count); |
Parameters : | dest Target memory area src Original memory area count Bytes to copy |
Return value : | Point to dest The pointer to |
Memmove And the function memcpy Functions function the same , But just copy memory space , Do not deal with spatial overlap .Memmove Can deal with spatial overlap . When dest and src When overlapping , Can still handle , but src Content changes .
6、 ... and 、memcmp
name : | memcmp |
function : | Compare the characters of two memory spaces |
The header file : | #include <memory.h> |
Function primitives : | int memcmp(void *buf1,void *buf2,unsigned int count); |
Parameters : | buf1 Memory area buf2 Memory area count The number of characters to compare |
Return value : | See below |
Memcmp Will compare memory areas buf1 and buf2 Before count Bytes .Memcmp Back basis ASCLL Code table sequence comparison . When buf1<buf2 when , return <0; When buf1=buf2 when , return 0; When buf1>buf2 when , return >0.
边栏推荐
- Draw Bezier curve through screen interaction
- 嵌入式c语言开发之宏定义求两个数的最大值的使用技巧
- (15) [driver development] over written copy
- MFC IniFile Unicode mode reading method
- New developments in Data Governance: what is the impact of the EU's Data Governance Research Report on China
- VIM configuring golang development environment
- (2022 Niuke multi school) D-Link with game glitch (SPFA)
- Data too long for column ‘data‘ at row 1以及设置成longblob造成的乱码解决。node-mysql
- EOL offline sequence based on iso13209 (Otx)
- (2022牛客多校)D-Link with Game Glitch(spfa)
猜你喜欢
![Get URL of [url reference]? For the following parameters, there are two ways to get the value of the corresponding parameter name and convert the full quantity to the object structure](/img/78/2a4e9d49bee8ef839d9d86fc7c3c83.png)
Get URL of [url reference]? For the following parameters, there are two ways to get the value of the corresponding parameter name and convert the full quantity to the object structure
![(15) [driver development] over written copy](/img/1c/68dfff5671add1fe91567e4df34135.png)
(15) [driver development] over written copy

EOL offline sequence based on iso13209 (Otx)

sqlilabs less-29

sqlilabs less-29

同条网线电脑正常上网,手机连接wifi成功,但是无法访问互联网

Softing pnGate系列网关:将PROFIBUS总线集成到PROFINET网络

(2022牛客多校)D-Link with Game Glitch(spfa)

Prometheus operator configures promethesrule alarm rules

What are the ways to realize web digital visualization?
随机推荐
[daily practice] day (14)
R language Visual scatter diagram, geom using ggrep package_ text_ The repl function avoids overlapping labels between data points (set the hJust parameter to show that labels of all data points are a
sqlilabs less-28~less-8a
sqlilabs less-29
基于ISO13209(OTX)实现EOL下线序列
R language uses LM function to build multiple linear regression model and write regression equation according to model coefficient
VO, dto, do, Po distinction and use
Idea commonly used 10 shortcut keys
VBA common objects
HTB-Optimum
Typedef usage and template
(15) [driver development] over written copy
ARM裸板调试之JTAG调试源码级调试
Use abp Zero builds a third-party login module (4): wechat applet development
Baidu, Alibaba, Tencent, who fell first?
R language uses rowmedians function to calculate the row data median value of all data rows in dataframe
阻塞队列分析
【C语言】指针和数组的深入理解(第一期)
(2022 Niuke multi School II) k-link with bracket sequence I (dynamic planning)
剑指 Offer 36. 二叉搜索树与双向链表