当前位置:网站首页>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 .
边栏推荐
- 十分钟彻底掌握缓存击穿、缓存穿透、缓存雪崩
- Implementation of Excel import and export functions
- MySQL shutdown is slow
- [算法] 剑指offer2 golang 面试题2:二进制加法
- [algorithm] sword finger offer2 golang interview question 8: the shortest subarray with a sum greater than or equal to K
- GNSS定位精度指标计算
- Compile GDAL source code with nmake (win10, vs2022)
- 2-year experience summary, tell you how to do a good job in project management
- Employment of cashier [differential constraint]
- IText 7 generate PDF summary
猜你喜欢
随机推荐
[算法] 剑指offer2 golang 面试题10:和为k的子数组
Record: solution of 404 error of servlet accessing database in dynamic web project
阿里云微服务(四) Service Mesh综述以及实例Istio
GPS高程拟合抗差中误差的求取代码实现
wsl常用命令
Error: symbol not found
NovAtel 板卡OEM617D配置步骤记录
Shortest Hamilton path (pressure DP)
几道高频的JVM面试题
Basic DOS commands
【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
【RTKLIB 2.4.3 b34 】版本更新简介一
GNSS定位精度指标计算
架构师怎样绘制系统架构蓝图?
第一人称视角的角色移动
Comparative analysis of the execution efficiency of MySQL 5.7 statistical table records
Record: I accidentally wrote a recursion next time
Exception: ioexception:stream closed
121 distributed interview questions and answers
[algorithm] sword finger offer2 golang interview question 9: subarray with product less than k