当前位置:网站首页>动态内存管理
动态内存管理
2022-07-25 18:26:00 【菜菜小蒙】
常用动态内存函数介绍
目录
(1)malloc和free
void* malloc(size_t size);这个函数向内存申请一块连续可用的空间,并返回指向这块空间的指针。
该函数如果开辟成功,则会返回指向开辟好空间的指针;如果开辟失败,则返回一个NULL指针,因此malloc的返回值一定要做检查。返回类型是 void* 所以使用时根据用户需求进行类型转换。
相应的,C语言提供了另外一个函数free,专门是用来做动态内存的释放和回收的,函数原型如下:
void free (void* ptr); free函数用来释放动态开辟的内存。
如果参数 ptr 指向的空间不是动态开辟的,那free函数的行为是未定义的。如果参数 ptr 是NULL指针,则不会进行任何操作。
例:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* p = (int*)malloc(40);
if (p != NULL)
{
for (int i = 0; i < 10; i++)
{
*(p + i) = i;
}
for (int j = 0; j < 10; j++)
{
printf("%d ", p[j]);
}
}
free(p);
p = NULL;
return 0;
}运行结果: ![]()
这里使用malloc函数先申请了40个字节的空间,再将空间转换为int *类型,使用整形指针p来接收。之后使用循环对申请的空间进行赋值以及打印。在这里*(p+i)与p[i]的效果相同。之后释放动态内存并把指针p置空。
(2)calloc
void* calloc (size_t num, size_t size);函数的功能是为 num 个大小为 size 的元素开辟一块空间,并且把空间的每个字节初始化为0。与函数 malloc 的区别只在于 calloc 会在返回地址之前把申请的空间的每个字节初始化为全0。
例:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* p = (int*)calloc(10, sizeof(int));
if (p != NULL)
{
for (int i = 0; i < 10; i++)
{
printf("%d ", p[i]);
}
printf("\n");
for (int j = 0; j < 10; j++)
{
*(p + j) = j;
}
for (int k = 0; k < 10; k++)
{
printf("%d ", p[k]);
}
}
free(p);
p = NULL;
return 0;
}运行结果:
![]()
首先使用calloc函数申请十个整形大小的空间。利用循环对申请的空间进行输出,对比mallco函数,我们可以得出calloc函数会对申请的空间进行初始化为0。之后对申请的空间进行赋值以及打印,释放动态内存并把指针p置空。
(3)realloc
void* realloc (void* ptr, size_t size);realloc函数的出现让动态内存管理更加灵活。该函数可以对动态内存空间进行调整。
ptr 是要调整的内存地址,size 调整之后新大小,返回值为调整之后的内存起始位置。
这个函数调整原内存空间大小的基础上,还会将原来内存中的数据移动到新的空间。如果空间足够大,那么realloc函数会直接在原有的空间后追加空间;如果空间不够大,那么在堆空间上另找一个合适大小的连续空间来使用。这样函数返回的是一个新的内存地址。因此使用realloc函数需要今生判断。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* p = (int*)malloc(40);
if (p != NULL)
{
for (int i = 0; i < 10; i++)
{
p[i] = i;
}
for (int j = 0; j < 10; j++)
{
printf("%d ", p[j]);
}
printf("\n");
}
int* ptr = (int*)realloc(p, 80);
if (ptr == NULL)
{
printf("申请失败\n");
return 1;
}
else
{
p = ptr;
for (int i = 0; i < 20; i++)
{
p[i] = i;
}
for (int j = 0; j < 20; j++)
{
printf("%d ", p[j]);
}
printf("\n");
}
free(p);
p = NULL;
return 0;
}运行结果:

本代码首先使用malloc函数申请40字节的内存空间,若指针p不为空,再进行赋值并打印。之后使用realloc函数进行扩容,调整后大小为80字节。因为realloc函数存在两种情况,如原有内存不够且找不到新的足够大的内存空间,realloc函数则会返回NULL。为了避免分配失败,原有空间丢失,故使用一个新的整形指针变量进行接收,若其为空则结束程序;若不为空,则将指针ptr赋值给指针p,并对新的内存空间进行赋值并打印。最后释放内存空间并将指针p置空。
边栏推荐
- Linux启动mysql报错
- Keil5 “Loading PDSC Debug Description Failed for STMicroelectronics STM32Hxxxxxxx”解决办法
- c语言---25 扫雷游戏
- 这是一张机器&深度学习代码速查表
- How to create an effective help document?
- 【华为机试真题】字符串匹配
- Interview shock: why does TCP need three handshakes?
- List conversion problem
- TESTNG中的并发测试invocationCount, threadPoolSize, timeOut的使用
- 【网页性能优化】SPA(单页面应用)首屏加载速度慢怎么办?
猜你喜欢

使用sqldeveloper连接mysql

Oracle uses impdp import to report an error: ora-39001: invalid parameter value ora-39000: dump file description error ora-39088: file name cannot contain path description

408 Chapter 2 linear table

网易严选库存中心设计实践

There was an error while marking a file for deletion

Stm8s003f3 internal flash debugging

SQL那些事

BiSeNet v1

win11下vscode 自动升级失败 There was an error while marking a file for deletion

408第二章线性表
随机推荐
Recognized by international authorities! Oceanbase was selected into the Forrester translational data platform report
BiSeNet v1
Tang's little helper
Bl602 development environment setup
Basic knowledge of documents
文件基础知识
7. Dependency injection
Analysis of regression problem, modeling and prediction
Oracle uses impdp import to report an error: ora-39001: invalid parameter value ora-39000: dump file description error ora-39088: file name cannot contain path description
PHP memory management mechanism and garbage collection mechanism
Application of current probe in ECU and electrical system current measurement
11.2-HJ86 求最大连续bit数
imx6 RTL8189FTV移植
testng执行顺序的3中控制方法
The use of invocationcount, threadpoolsize, timeout of concurrent tests in TestNG
How to add EXE file to boot
基于Caffe ResNet-50网络实现图片分类(仅推理)的实验复现
408第二章线性表
If you want to do a good job in software testing, you can first understand ast, SCA and penetration testing
【网页性能优化】SPA(单页面应用)首屏加载速度慢怎么办?