当前位置:网站首页>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 ;
边栏推荐
- Regular expression
- 【数据挖掘】生成模型和判别模型的区别及优缺点
- 【Code】剑指offer 03数组中重复的数字
- 更换IP地址常见的4种简单有效的方法
- Cross-lingual Transfer of Correlations between Parts of Speech and Gaze Features 阅读笔记
- 【软件开发规范四】《应用系统安全编码规范》
- 中心对称的二进制模式CSLBP,matlab
- 【纪中】2022.7.16 1432.输油管道
- 数据库系统原理与应用教程(055)—— MySQL 查询(十七):数学函数的用法
- 《暗黑破坏神:不朽》手游如何多开搬砖及新手入门搬砖攻略
猜你喜欢

EasyRecovery15下载量高的恢复率高的数据恢复软件

Spine_附件皮肤
![[RTOS training camp] about classes and Q & A](/img/ea/32a4f4a5be29afcd0a68a0bcf0169f.jpg)
[RTOS training camp] about classes and Q & A

android sqlite先分组后排序左连查询

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

PyCharm在创建py文件时自动添加头部注释

Win11打不开自带杀毒软件怎么办?win11自带杀毒功能打不开

换ip软件的用途很广及原理 动态IP更换的四种方法来保护网络隐私

腾讯员工晒出薪资:真实985毕业薪资,大家看我还有救吗?网友:日薪?

代理IP服务器如何保证自身在网络中的信息安全呢
随机推荐
Subarray with 19 and K
[software development specification III] [software version naming Specification]
[RTOS training camp] learn C language from a higher perspective
[Go]三、最简单的RestFul API服务器
机器学习:贝叶斯网络
数据库系统原理与应用教程(053)—— MySQL 查询(十五):字符型函数的用法
MMOCR使用指南
[RTOS training camp] course learning methods and C language knowledge (pointer, structure, function pointer, linked list) and student questions
The Chinese input (Pinyin input method) component created by lvgl official +100ask enables lvgl to support Chinese input!
Ssd7 | embedded friendly target detection network, product landing
JDBC connection database (idea version)
985 associate professors in Colleges and universities sun their annual salary, and the provident fund tops the monthly salary of ordinary people. Netizen: it is worthy of being in Shanghai
Spine_附件皮肤
[software development specification iv] application system security coding specification
NIO简易示例
Paged query design of scenarios
Selenium assertion and JS actuator
加载dll失败
Modify CSV
matlab 移位操作基础