当前位置:网站首页>C语言:动态内存函数
C语言:动态内存函数
2022-07-27 14:27:00 【发发是只呆头鹅】
1、动态内存函数
1.1 malloc和free
malloc这个函数向内存申请一块连续可用的空间,并返回指向这块空间的指针。![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tjBAy9lP-1632745217929)(C:\Users\10371\AppData\Roaming\Typora\typora-user-images\image-20210927191647836.png)]](/img/d6/c444959cbdbc2005db23d48950072c.png)
参数为开辟字节的大小,返回类型为void*,空间开辟成功指针指向该空间的地址,开辟失败后指针为NULL,所以每次开辟空间后都需要判断是否开辟成功,我们来看一个例子:
int main()
{
int* ptr = NULL;
ptr = (int*)malloc(10 * sizeof(int));
if (NULL != ptr)//判断ptr指针是否为空
{
for (int i = 0; i < 10; i++)
{
*(ptr + i) = 0;
}
}
for (int i = 0; i < 10; i++)
printf("%d ", *(ptr + i));
free(ptr);
ptr = NULL;
return 0;
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-WfH1UIUx-1632745217931)(C:\Users\10371\AppData\Roaming\Typora\typora-user-images\image-20210927193142358.png)]](/img/64/6c43fc5944dc9f4fde4e12dafef86a.png)
开辟空间后,到最后要free开辟的空间,还要将其职位NULL。
1.2 calloc
calloc 函数也用来动态内存分配,函数的功能是为 num 个大小为 size 的元素开辟一块空间,并且把空间的每个字节初始化为0。![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-GdlR4rkB-1632745217932)(C:\Users\10371\AppData\Roaming\Typora\typora-user-images\image-20210927193642191.png)]](/img/5e/ecdbbf3d6fa056f7035e95aacd7db8.png)
其与malloc的区别就是calloc会将开辟好的空间的元素初始化赋值为0,我们来看一个例子:
int main() {
int* ptr = NULL;
ptr = (int*)calloc(10, sizeof(int));
for (int i = 0; i < 10; i++)
printf("%d ", *(ptr + i));
free(ptr);
ptr = NULL;
return 0;
}
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FcFYHTzu-1632745217933)(C:\Users\10371\AppData\Roaming\Typora\typora-user-images\image-20210927193928537.png)]](/img/f9/512993a59d88b96cb3931637f3c7b3.png)
1.3 realloc
realloc函数的出现让动态内存管理更加灵活,有时会我们发现过去申请的空间太小了,有时候我们又会觉得申请的空间过大了,那为了合理的时 候内存,我们一定会对内存的大小做灵活的调整。那 realloc 函数就可以做到对动态开辟内存大 小的调整。
memblock为需要调整大小的位置,size为调整后的新大小,返回值为调整之后的内存起始位置。我们来看一个例子:
int main()
{
int* ptr = NULL;
ptr = (int*)malloc(10 * sizeof(int));
if (NULL != ptr){
//判断ptr指针是否为空
for (int i = 0; i < 10; i++){
*(ptr + i) = i;
}
}
for (int i = 0; i < 10; i++){
printf("%d ", *(ptr + i));
}
int* p = NULL;
p = realloc(ptr, 20 * sizeof(20)); //将空间调整为20个int类型的大小
if (NULL != p) {
ptr = p;
for (int i = 10; i < 20; i++)
*(ptr + i) = i;
}
for (int i = 0; i < 20; i++)
printf("%d ", *(ptr + i));
free(ptr);
ptr = NULL;
return 0;
}

realloc在扩大空间时会遇到两种情况,
(1)原来空间后面的空间足够,直接在原来的空间后面开始扩容。
(2)原来空间后面的空间不够,寻找一块新的空间,并将原来空间中的值赋到新的空间中,并且释放原来的空间。
2、柔性数组
结构中的最后一个元素允许是未知大小的数组,这就叫做柔性数组成员。
2.1 柔性数组的特点:
- 结构中的柔性数组成员前面必须至少一个其他成员。
- sizeof 返回的这种结构大小不包括柔性数组的内存。
- 包含柔性数组成员的结构用malloc ()函数进行内存的动态分配,并且分配的内存应该大于结构的大小,以适应柔性数组的预期大小。
2.2 柔性数组的使用
来看代码:
struct soft_arry {
int i;
int arr[];
}*p;
int main() {
struct soft_arry* p = (struct soft_arrya*)malloc(sizeof(struct soft_arry) + 100 * sizeof(int));
//开辟的空间使得arr数组可以容纳下100个int类型的数据
if(p != NULL){
for (int i = 0; i < 100; i++) {
p->arr[i] = i;
}
for (int i = 0; i < 100; i++) {
printf("%d ", p->arr[i]);
}
}
free(p);
p = NULL;
return 0;
}

边栏推荐
- 【剑指offer】面试题52:两个链表的第一个公共节点——栈、哈希表、双指针
- Leetcode 456.132 mode monotone stack /medium
- Go language learning notes (1)
- Reading notes of lifelong growth (I)
- Unity performance optimization ----- occlusion culling of rendering optimization (GPU)
- Network equipment hard core technology insider router Chapter 16 dpdk and its prequel (I)
- 西瓜书《机器学习》阅读笔记之第一章绪论
- Huayun data creates a perfect information technology and innovation talent training system to help the high-quality development of information technology and innovation industry
- Leetcode 81. search rotation sort array II binary /medium
- 【剑指offer】面试题53-Ⅰ:在排序数组中查找数字1 —— 二分查找的三个模版
猜你喜欢

Watermelon book machine learning reading notes Chapter 1 Introduction

/dev/loop1占用100%问题

md 中超链接的解析问题:解析`this.$set()`,`$`前要加空格或转义符 `\`
![[0 basic operations research] [super detail] column generation](/img/cd/f2521824c9ef6a50ec2be307c584ca.png)
[0 basic operations research] [super detail] column generation

Fluent -- layout principle and constraints

QT (IV) mixed development using code and UI files

后台返回来的是这种数据,是什么格式啊

Huayun data creates a perfect information technology and innovation talent training system to help the high-quality development of information technology and innovation industry

Spark Filter算子在Parquet文件上的下推

QT (five) meta object properties
随机推荐
Record record record
使用Prometheus监控Spark任务
USB interface electromagnetic compatibility (EMC) solution
使用解构交换两个变量的值
【剑指offer】面试题53-Ⅰ:在排序数组中查找数字1 —— 二分查找的三个模版
Deveco studio2.1 operation item error
C:什么是函数中的返回值(转)
Network equipment hard core technology insider router chapter Cisco asr9900 disassembly (I)
【剑指offer】面试题41:数据流中的中位数——大、小堆实现
2022-07-27 Daily: IJCAI 2022 outstanding papers were published, and 298 Chinese mainland authors won the first place in two items
华云数据打造完善的信创人才培养体系 助力信创产业高质量发展
Inside router of network equipment hard core technology (10) disassembly of Cisco asr9900 (4)
QT (five) meta object properties
Spark3中Catalog组件设计和自定义扩展Catalog实现
后台返回来的是这种数据,是什么格式啊
Selenium reports an error: session not created: this version of chromedriver only supports chrome version 81
Transactions_ Basic demonstrations and transactions_ Default auto submit & manual submit
Sword finger offer merges two sorted linked lists
js使用一元运算符简化字符串转数字
【剑指offer】面试题42:连续子数组的最大和——附0x80000000与INT_MIN