当前位置:网站首页>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.
边栏推荐
- Vs code的安装步骤及环境配置
- vim编辑器使用
- Why is Google's internal tools not suitable for you?
- Helm chart for Kubernetes
- 指针
- 365 day challenge leetcode 1000 questions - day 037 elements and the maximum side length of squares less than or equal to the threshold + the number of subsequences that meet the conditions
- Introduction to array learning simple question sum of two numbers
- Teardown's method of lifting the time limit
- 研发效能|Kubernetes核心技术剖析和DevOps落地经验
- 递归的基本原理
猜你喜欢

【C语言系列】—文件操作详解(上)

AiTalk创始人梁宇淇:镜像连接虚拟与现实的纽带

英伟达周锡健:设计到数字营销的最后一公里

365天挑战LeetCode1000题——Day 041 二分查找完结纪念 + 第 N 个神奇数字 + 在线选举

CMU15-213 Shell Lab实验记录

C语言数组入门到精通(数组精讲)

Vs code的安装步骤及环境配置

365 day challenge leetcode 1000 questions - day 041 two point search completion anniversary + nth magic number + online election

C语言求字符串的长度

R & D efficiency | analysis of kubernetes' core technology and Devops' landing experience
随机推荐
Xiaolu Inn - Trailer
分配内存:malloc()和free()
容器安全开源检测工具--问脉 VeinMind(镜像后门、恶意样本、敏感信息、弱口令等)
冒泡排序 C语言
MySQL的基础概念+数据库系统结构+拓展延申+基础命令学习
Alibaba cloud architect details nine trends in the game industry
365天挑战LeetCode1000题——Day 035 每日一题 + 二分查找 13
C语言数组典型应用代码详细讲解—高手误入(逐步代码详解)
【C语言系列】— 不创造第三个变量,实现两个数的交换
C语言 一级指针
【C语言系列】— 打印100~200之间的素数
直播预告:京东云DevOps与JFrog制品库的融合
Handwritten student management system
CMake 设置vs启动运行环境路径
osgSimplegl3例子分析
Cryengine5 shader debugging
Container security open source detection tool - veinmind (mirror backdoor, malicious samples, sensitive information, weak password, etc.)
365天挑战LeetCode1000题——Day 037 元素和小于等于阈值的正方形的最大边长 + 满足条件的子序列数目
哈夫曼树以及哈夫曼编码在文件压缩上的应用
【C语言系列】—三种方法模拟实现strlen库函数的方法