当前位置:网站首页>4.30 dynamic memory allocation notes
4.30 dynamic memory allocation notes
2022-07-06 13:07:00 【犇犇犇犇犇犇犇】
Why is there a memory allocation
int a=10;// local variable
int g_a=10;// Global variables
1. When creating a variable, memory will be requested
The memory is divided into a large area
The stack area
Heap area
Static zone
The stack area holds local variables , Formal parameters of functions, etc
Heap storage dynamic memory allocation space
The static area stores global variables , Static variables, etc
2. Creating an array can be a local variable or a global variable
int arr[10];
But to create the memory of the array in this way, we must first give the length of the array ,C Languages cannot create variable length arrays (C99 Your compiler can )int arr[n]; So in some cases, it's not easy for us to apply for memory , For example, when storing the information of students in a class , The number of students will inevitably change, which will lead to a waste of space or insufficient space applications , Then we need dynamic memory allocation .
malloc
void* malloc (size_t size);
int* p=(int*)malloc(sizeof(int));
if(p==NULL)
{
printf("%s\n",strerror(errno));
}
else
{
int i=0;
for(i=;i<10;i++)
{
*(p+i)=i;
}
for(i=0;i<10;i++)
{
printf("%d",*(p+i));
}
}
free(p);
This is it. malloc Format of function void* It's a pointer type , When you apply for memory space, send the first address to you . however malloc It will fail , For example, when you only have 4G But you have to apply 8G This is the memory that will fail to apply , If the application fails, a null pointer will be returned , So we have to deal with it .
free
When you malloc Successful application , Then you need to free up space when you are not using it , Give space back to the operating system ,free This function is the function . This is particularly important in large projects, otherwise it will cause memory leakage , But in ordinary practice, if it doesn't, it won't have any effect , Because the operating system will automatically reclaim space at the end of the process . When you release the memory of a pointer, remember to set the pointer NULL, Otherwise, the pointer can actually find that space .
*free Only the memory developed dynamically
calloc
void* calloc (size_t num, size_t size);
This function follows malloc It works the same , It just assigns each byte of the applied space to zero when opening up space ( Initialize to zero ). And there are some subtle differences in format expression , One is to directly calculate the memory of the total elements , One is to calculate how many elements there are and how big each element is .
realloc
p=(int*)realloc(p,40);//40 Previous p Represents the address of the original space
realloc You can adjust the size of dynamic memory , When you malloc When the requested memory is insufficient, you can use realloc To adjust the dynamically opened memory , When you use the same pointer realloc Pay attention to this when adjusting memory . When you malloc There is not enough follow-up space for you realloc When you apply for adjustment, you will reopen a space in other places and copy all the data , At this time, the returned address may be different from the address of the original space , The original address will be discarded , And when realloc When it fails , Returns an empty , The original address will also be discarded , So we have to use a new variable to accept realloc The return value of the function .
realloc Precautions for use :
1. If p After the space pointed to, there is enough memory space to append , Then... Is added directly , After the return p
2. If p There is not enough memory space to append after the space pointed to , be realloc Function will find a new content area , Open up a space to meet the needs , And copy the data in the original memory , Free up old memory space , Finally, return the newly opened memory space address .
边栏推荐
- Several high-frequency JVM interview questions
- WSL common commands
- Detailed explanation of balanced binary tree is easy to understand
- 《软件测试》习题答案:第一章
- 记录:下一不小心写了个递归
- Containers and Devops: container based Devops delivery pipeline
- Lean product development - Lean Software Development & lean product development
- KF UD分解之伪代码实现进阶篇【2】
- 一文搞定 UDP 和 TCP 高频面试题!
- [algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
猜你喜欢
![[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数](/img/64/0f352232359c7d44f12b20a64c7bb4.png)
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数

面渣逆袭:Redis连环五十二问,三万字+八十图详解。
![[算法] 剑指offer2 golang 面试题10:和为k的子数组](/img/63/7422489d09a64ec9f0e79378761bf1.png)
[算法] 剑指offer2 golang 面试题10:和为k的子数组

Implementation of Excel import and export functions

121道分布式面试题和答案

Wechat applet development experience

国企秋招经验总结

继承和多态(下)
![Fundamentals of UD decomposition of KF UD decomposition [1]](/img/e9/564e0163c3756c0ba886913f1cfaef.jpg)
Fundamentals of UD decomposition of KF UD decomposition [1]

系统设计学习(一)Design Pastebin.com (or Bit.ly)
随机推荐
Error: symbol not found
[算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
Detailed explanation of balanced binary tree is easy to understand
一文搞定 UDP 和 TCP 高频面试题!
[GNSS] robust estimation (robust estimation) principle and program implementation
地球围绕太阳转
What are the functions and features of helm or terrain
Ten minutes to thoroughly master cache breakdown, cache penetration, cache avalanche
记录:动态Web项目servlet访问数据库404错误之解决
MySQL shutdown is slow
架构师怎样绘制系统架构蓝图?
2022国赛Re1 baby_tree
Record: newinstance() obsolete replacement method
rtklib单点定位spp使用抗差估计遇到的问题及解决
如何保障 MySQL 和 Redis 的数据一致性?
Alibaba cloud side: underlying details in concurrent scenarios - pseudo sharing
KF UD decomposition pseudo code implementation advanced [2]
[Chongqing Guangdong education] Shandong University College Physics reference materials
Sharing ideas of on-chip transplantation based on rtklib source code
【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现