当前位置:网站首页>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 .
边栏推荐
- 面试必备:聊聊分布式锁的多种实现!
- 【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
- 初识C语言(上)
- [algorithm] sword finger offer2 golang interview question 1: integer division
- Usage differences between isempty and isblank
- MySQL 30000 word essence summary + 100 interview questions, hanging the interviewer is more than enough (Collection Series
- 第一人称视角的角色移动
- Wechat applet development experience
- [算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
- 2022国赛Re1 baby_tree
猜你喜欢
Matlab读取GNSS 观测值o文件代码示例
Detailed explanation of balanced binary tree is easy to understand
继承和多态(上)
阿里云微服务(四) Service Mesh综述以及实例Istio
Role movement in the first person perspective
Iterable、Collection、List 的常见方法签名以及含义
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)
Prove the time complexity of heap sorting
国企秋招经验总结
随机推荐
[算法] 剑指offer2 golang 面试题9:乘积小于k的子数组
Error: symbol not found
基于rtklib源码进行片上移植的思路分享
MySQL 30000 word essence summary + 100 interview questions, hanging the interviewer is more than enough (Collection Series
《软件测试》习题答案:第一章
[untitled]
PRIDE-PPPAR源码解析
抽象类和接口
系统设计学习(一)Design Pastebin.com (or Bit.ly)
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
Prove the time complexity of heap sorting
Implementation of Excel import and export functions
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
Employment of cashier [differential constraint]
rtklib单点定位spp使用抗差估计遇到的问题及解决
记录:Navicat Premium初次无法连接数据库MySQL之解决
架构师怎样绘制系统架构蓝图?
Record: newinstance() obsolete replacement method
记录:初次cmd启动MySQL拒接访问之解决
RTKLIB: demo5 b34f. 1 vs b33