当前位置:网站首页>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
- Wechat applet development experience
- [algorithm] sword finger offer2 golang interview question 9: subarray with product less than k
- FairyGUI循環列錶
- 系统设计学习(二)Design a key-value cache to save the results of the most recent web server queries
- 记录:动态Web项目servlet访问数据库404错误之解决
- 【干货】提升RTK模糊度固定率的建议之周跳探测
- 系统设计学习(一)Design Pastebin.com (or Bit.ly)
- 记录:下一不小心写了个递归
- [algorithm] sword finger offer2 golang interview question 7: 3 numbers with 0 in the array
猜你喜欢
[算法] 剑指offer2 golang 面试题4:只出现一次的数字
[算法] 剑指offer2 golang 面试题1:整数除法
【干货】提升RTK模糊度固定率的建议之周跳探测
NovAtel 板卡OEM617D配置步骤记录
RTKLIB: demo5 b34f.1 vs b33
如何保障 MySQL 和 Redis 的数据一致性?
121道分布式面试题和答案
[algorithm] sword finger offer2 golang interview question 12: the sum of the left and right sub arrays is equal
[untitled]
【无标题】
随机推荐
音乐播放(Toggle && PlayerPrefs)
编辑距离(多源BFS)
Ten minutes to thoroughly master cache breakdown, cache penetration, cache avalanche
Code example of MATLAB reading GNSS observation value o file
One article to get UDP and TCP high-frequency interview questions!
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
异常:IOException:Stream Closed
[GNSS data processing] Helmert variance component estimation analysis and code implementation
Chromatic judgement bipartite graph
十分鐘徹底掌握緩存擊穿、緩存穿透、緩存雪崩
Mixed use of fairygui button dynamics
String类
Sharing ideas of on-chip transplantation based on rtklib source code
[Chongqing Guangdong education] reference materials for regional analysis and planning of Pingdingshan University
【干货】提升RTK模糊度固定率的建议之周跳探测
闇の連鎖(LCA+树上差分)
RTKLIB: demo5 b34f.1 vs b33
MySQL 30000 word essence summary + 100 interview questions, hanging the interviewer is more than enough (Collection Series
KF UD decomposition pseudo code implementation advanced [2]
《软件测试》习题答案:第一章