当前位置:网站首页>Advanced C language (I) dynamic memory allocation
Advanced C language (I) dynamic memory allocation
2022-07-26 01:16:00 【Princess Kaka】
The header file :stdlib.h string.h
One . Request dynamic allocation of memory malloc
stdlib.h Define function in void* malloc(size_t size), Apply to the system The size is size Of memory space . The return is void*, Convert to the required pointer type when using . If the application fails , return NULL.
void A pointer is a special kind of pointer , Expressed as “ No type pointer ”, Any type of pointer can be assigned directly to void The pointer , There is no need for other related cast types , Even so , But this does not mean that you can cast void Pointers are directly assigned to other types of pointers , To cast .
Two . Free dynamically allocated memory free()
#include<stdio.h>
#include<stdlib.h>
int main(){
int n;
scanf("%d",&n);
//int arr[n];
//malloc Write input array
int* arr = malloc(sizeof(int)*n);
for(int i=0;i<n;i++){
scanf("%d",&arr[i]);
}
for(int i=0;i<n;i++){
printf("%d",&arr[i]);
}
free(arr);
arr=NULL;
// Do not set the pointer to NULL Yes. . But if you happen to new/malloc Apply for allocation to this memory , Unpredictable things will happen .
// So the released pointer is for safety , To set it to NULL, Otherwise, it is easy to cause the appearance of wild pointer .
}3、 ... and . Initialize dynamic memory allocation calloc
stay Linux in malloc and calloc All will be assigned
int* arr = (int*)calloc(n,sizeof(int));
free(arr);Approximate to
int arr[n] = {0};Four . Resize memory realloc
void* realloc (void* ptr, size_t size);- If there is enough memory space behind the current memory segment , Then expand this memory directly ,
realloc()Return to the original first address ; - If there is not enough memory space behind the current memory segment , Then the system will re apply to the memory tree for a suitable space , And release the data blocks in the original space , and
realloc()The first address of the heap space reapplied will be returned ; - If the creation fails , return
NULL, At this time, the original pointer is still valid ;
边栏推荐
猜你喜欢

Linear relationship between vectors

什么是信息化?什么是数字化?这两者有什么联系和区别?

Failed to load DLL

"Yuanqi Cola" is not the end point, "China Cola" is

【纪中】2022.7.16 1432.输油管道

Android SQLite first groups and then sorts left concatenated queries
![[RTOS training camp] problems of evening students](/img/4a/9d781a28751c15e9e42cd5743e97db.jpg)
[RTOS training camp] problems of evening students

# 浏览器开发使用技巧

Game thinking 17: Road finding engine recast and detour learning II: recast navigation grid generation process and limitations

What are the ways to quickly improve programming skills in the process of programming learning?
随机推荐
点屏注意事项
Embedded development: tips and tricks -- seven tips for designing powerful boot loader
android sqlite先分组后排序左连查询
RHCE之at和crontab命令详解及chrony部署
[RTOS training camp] I2C and UART knowledge and preview arrangement + evening class questions
What is informatization? What is digitalization? What are the connections and differences between the two?
Force buckle 25. Turn over the linked list in a group of K
[Code] refers to the repeated number in the offer 03 array
MMOCR使用指南
场景之分页查询设计
Web middleware log analysis script 3.0 (shell script)
[software development specification III] [software version naming Specification]
MySQL pymysql operation
聚势|海泰方圆亮相第五届数字中国建设峰会
【RTOS训练营】站在更高的角度学习C语言
Mmocr usage guide
力扣 25. K 个一组翻转链表
Arthas watch 命令查看数组中对象的属性
Lua基础语法
第二届中国Rust开发者大会来啦,完整议程大曝光!