当前位置:网站首页>Allocate memory: malloc() and free()
Allocate memory: malloc() and free()
2022-07-29 05:28:00 【Ryan fitter】
Allocate memory :malloc() and free()
Memory storage categories have one thing in common : After determining which storage category to use , According to your own memory management rules , Scope and storage period will be automatically selected .
malloc() function
This function takes a parameter : Number of memory bytes required . use malloc() Create an array with the following code :
double * ptd;
ptd = (double *)malloc(30 * sizeof(double));
The above code is 30 individual double The value of type requests memory space , And set up ptd Point to this location . You can also use expressions ptd[0] Represents an array element .
Three ways to create arrays :
1. When you declare an array , The dimension of an array is represented by a constant expression , Use the array name to access the elements of the array . You can create this array with static memory or automatic memory .
2. When declaring a variable length array , The dimension of an array is represented by a variable expression , Use the array name to access the elements of the array . Arrays with this feature can only be created in automatic memory .
3. Declare a pointer , call malloc(), Assign its return value to the pointer , Using pointers to access elements of an array . The pointer can be static or automatic .
free() function
Usually malloc() To work with free() Matching use of . among free() The argument to the function is before malloc() Return address , Mainly to release malloc() Allocated memory . Call... At the end of the function free() Functions can avoid Memory leak The problem of
exit() function
If memory allocation fails , You can call exit() The function ends the program , Its prototype is also stdlib.h in , The standard provides two return values to ensure normal operation in all operating systems :
EXIT_SUCCESS( perhaps , amount to 0) Indicates the end of a normal program .
EXIT_FAILURE Indicates that the program aborts abnormally .
Other operating systems also accept integer values that indicate other operational errors .
Run a piece of code and use the above functions to realize the program :
// Assign arrays dynamically
#include <stdio.h>
#include <stdlib.h>// by malloc()、free() Provide prototypes
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 Now point to max Array of elements
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;
}
Running results :
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.
边栏推荐
- QML custom tabbar
- AiTalk创始人梁宇淇:镜像连接虚拟与现实的纽带
- 【赛事预告】云上开发,高效智能——第二届阿里云ECS CloudBuild开发者大赛即将启动
- Vs code的安装步骤及环境配置
- 抽象类与接口
- 副作用和序列点
- Best practices of JD cloud Distributed Link Tracking in financial scenarios
- osgSimplegl3例子分析
- 510000 prize pool invites you to fight! The second Alibaba cloud ECS cloudbuild developer competition is coming
- The latest tank battle 2022 - full development notes-3
猜你喜欢
Li Yan, CEO of parallel cloud: cloudxr, opens the channel to the metauniverse
osgSimplegl3结合RenderDoc工具
三次握手四次挥手针对面试总结
英伟达周锡健:设计到数字营销的最后一公里
C language handwritten qq-ai version
365天挑战LeetCode1000题——Day 039 完全二叉树插入器 + 寻找峰值 II + 快照数组
510000 prize pool invites you to fight! The second Alibaba cloud ECS cloudbuild developer competition is coming
The road to success in R & D efficiency of 1000 person Internet companies
51万奖池邀你参战!第二届阿里云ECS CloudBuild开发者大赛来袭
Vs code的安装步骤及环境配置
随机推荐
osg进阶-序
2022数学建模竞赛暑期培训讲座——最优化方法:目标规划
存储类别
What is_ GLIBCXX_ VISIBILITY(default)
MySQL的详细安装使用教程(保姆式安装图文讲解)
抢先预约 | 阿里云无影云应用线上发布会预约开启
AiTalk创始人梁宇淇:镜像连接虚拟与现实的纽带
CMake 设置vs启动运行环境路径
167. Sum of two numbers II - enter an ordered array
167. 两数之和 II - 输入有序数组
Teardown's method of lifting the time limit
ANSI C类型限定符
Live broadcast Preview: integration of JD cloud Devops and jfrog product library
适创科技以云仿真平台,支持“中国智造”升级
C语言求字符串的长度
Cryengine3 debugging shader method
webgl1.0下texture2D和texture2DProj区别
01-01-osg GL3 环境搭建
C语言 一维数组
Alibaba cloud architect Liang Xu: MES on cloud box helps customers quickly build digital factories