当前位置:网站首页>分配内存:malloc()和free()
分配内存:malloc()和free()
2022-07-29 05:08:00 【Ryan菲特】
分配内存:malloc()和free()
内存的存储类别有一个共同之处:在确定用那种存储类别之后,根据自己制定好的内存管理规则,将自动选择器作用域和存储期。
malloc()函数
该函数接受一个参数:所需的内存字节数。用malloc()创建一个数组如下代码:
double * ptd;
ptd = (double *)malloc(30 * sizeof(double));
以上代码为30个double类型的值请求内存空间,并设置ptd指向该位置。同样可以使用表达式ptd[0]表示数组元素。
创建数组的三种方式:
1.声明数组时,用常量表达式表示数组的维度,用数组名访问数组的元素。可以用静态内存或自动内存创建这种数组。
2.声明变长数组时,用变量表达式表示数组的维度,用数组名访问数组的元素。具有这种特性的数组只能在自动内存中创建。
3.声明一个指针,调用malloc(),将其返回值赋给指针,使用指针访问数组的元素。该指针可以是静态的或自动的。
free()函数
通常malloc()要与free()配套使用。其中free()函数的参数是之前malloc()返回的地址,主要是为了释放malloc()分配的内存。在函数末尾处调用free()函数可以避免内存泄漏的问题
exit()函数
如果内存分配失败,可以调用exit()函数结束程序,其原型也在 stdlib.h 中,标准提供了两个返回值以保证在所有操作系统中都能正常工作:
EXIT_SUCCESS(或者,相当于0)表示普通的程序结束。
EXIT_FAILURE表示程序异常中止。
其他操作系统还接受一些表示其他运行错误的整数值。
运行一段代码利用以上函数实现程序:
//动态分配数组
#include <stdio.h>
#include <stdlib.h>//为malloc()、free()提供原型
int main(void)
{
double * ptd;
int max;
int number;
int i = 0;
puts("What is the maxium nuber of type double enteries?");
if(scanf("%d",&max) != 1)
{
puts("Number not correctly entered -- bye.");
exit(EXIT_FAILURE);
}
ptd = (double *)malloc(max * sizeof(double));
if(ptd == NULL)
{
puts("Memory allocation failed. Goodbye.");
exit(EXIT_FAILURE);
}
//ptd 现在指向有max个元素的数组
puts("Enter the Values (q to quit):");
while(i < max && scanf("%lf",&ptd[i]) == 1)
++i;
printf("Here are your %d enteries:\n", number = i);
for (i = 0; i < number;i++)
{
printf("%7.2f", ptd[i]);
if(i % 7 == 6)
putchar('\n');
}
if(i % 7 != 0)
putchar('\n');
puts("Done.");
free(ptd);
return 0;
}
运行结果:
What is the maxium nuber of type double enteries?
5
Enter the Values (q to quit):
20 30 35 25 40 80
Here are your 5 enteries:
20.00 30.00 35.00 25.00 40.00
Done.
边栏推荐
- vs2019编译cryengine失败问题处理
- 京东云金秋上云特惠进行中!扫码参与活动
- 167. 两数之和 II - 输入有序数组
- CMU15-213 Malloc Lab实验记录
- Container security open source detection tool - veinmind (mirror backdoor, malicious samples, sensitive information, weak password, etc.)
- QT learning: qdropevent drag event
- 最新坦克大战2022-全程开发笔记-3
- C language handwritten qq-ai version
- Is Huatai Securities an AA level securities company? How about this company? Is it safe to open an account?
- Unity3d - the object is too far away to see
猜你喜欢

6.2 function-parameters

Self join and joint query of MySQL

小白高薪捷径-Qt开发游戏—贪吃蛇

How mongodb inserts, deletes and updates documents

千人规模互联网公司研发效能成功之路

Arfoundation starts from zero 9-ar anchor

京东云分布式链路追踪在金融场景的最佳实践

200 多家 ISV 入驻!阿里云计算巢发布一周年

C语言函数实现输出I love you

Container security open source detection tool - veinmind (mirror backdoor, malicious samples, sensitive information, weak password, etc.)
随机推荐
Qt版的贪食蛇游戏项目
More than 200 ISVs have settled in! The first anniversary of Alibaba cloud computing nest
C how to realize simple factory mode
CSDN的md编辑器如何输入上下标?公式和非公式的输入方式不一样
365天挑战LeetCode1000题——Day 038 公交站间的距离 + 基于时间的键值存储 + 转变数组后最接近目标值的数组和 + 有界数组中指定下标处的最大值
Getting started with solidity
直播预告:京东云DevOps与JFrog制品库的融合
Arfoundation starts from scratch 3- create an arfoundation project
Live broadcast preview | how to improve enterprise immunity through "intelligent edge security"?
The latest tank battle 2022 - Notes on the whole development -2
Rimworld通过SteamCMD上传创意工坊的方法
JD cloud and Forrester consulting released a hybrid cloud report that cloud Nativity has become a new engine driving industrial development
MySQL many to many relationship, grouping and splicing to query multiple data to one data
2022数学建模竞赛暑期培训讲座——最优化方法:目标规划
Scikit learn -- steps and understanding of machine learning application development
Soft link & hard link
MySQL sorts the queried result set according to the specified sequence
vs2019编译cryengine失败问题处理
C语言数组典型应用代码详细讲解—高手误入(逐步代码详解)
英伟达周锡健:设计到数字营销的最后一公里