当前位置: 首页 > news >正文

准备工作之动态内存分配[基于郝斌课程]

定义一块内存可以用数组定义,也可以动态分配:

使用数组定义一块内存,则该块内存是静态的,也就是一旦定义之后,这块内存的大小就固定了,例如,数组元素个数是5,则定义后,这=这块内存大小就是5,不能再改变

但是用malloc动态分配的话,这块内存的大小就由我们自己来定义了,例如定义大小为5的内存块,使用完毕后,需要一个大小为3的内存块,就可以先使用malloc来定义一个大小为3的内存块,如何使用free来释放此块内存,之后再次使用malloc来定义大小为3的内存块,最后需要用free来释放该块内存


/*
@file      main.c
@brief     数据结构预备知识之动态内存分配
@author    EricsT (EricsT@163.com)
@version   v1.0.0
@date      2025-09-20
@history   2025-09-20 EricsT - 新建文件
*/#include <stdio.h>
#include <malloc.h>int main(void)
{int a[5] = { 4, 10, 2, 8, 6 };int len;printf("请输入您需要分配的数组长度:len = ");scanf("%d", &len);//malloc函数只返回首字节地址int* ptr = (int*)malloc(sizeof(int) * len);//分配内存*ptr = 4;//类似于a[0] = 4*(ptr + 1) = 10;//类似于a[1] = 10;ptr[2] = 2;//类似于a[2] = 2;printf("%d %d %d\n\n\n", ptr[0], ptr[1], ptr[2]);for (int i = 0; i < len; ++i)scanf("%d", ptr + i);for (int i = 0; i < len; ++i)printf("%d\n", ptr[i]);free(ptr);//释放内存return 0;
}

在以下程序中,调用了 f() 函数时, j 所占内存是存在的,当 f() 函数调用结束后, j 所占的内存就不合法了,因为 j 是一个局部变量


/*
@file      main.c
@brief     数据结构预备知识之动态内存分配
@author    EricsT (EricsT@163.com)
@version   v1.0.0
@date      2025-09-20
@history   2025-09-20 EricsT - 新建文件
*/#include <stdio.h>int f();int main(void)
{int i = 10;i = f();printf("i = %d\n", i);return 0;
}int f()
{int j = 20;return j;
}

在以下程序中,掉用函数结束后,ptr都可以指向合法的内存块


/*
@file      main.c
@brief     数据结构预备知识之跨函数使用内存
@author    EricsT (EricsT@163.com)
@version   v1.0.0
@date      2025-09-20
@history   2025-09-20 EricsT - 新建文件
*/#include<stdio.h>
#include <malloc.h>void fun(int** p);int main(void)
{int* p;fun(&p);//调用完之后,p就指向合法的内存块return 0;
}
void fun(int** p)
{*p = (int*)malloc(4);//手动分配,不释放就会一直被占用
}

/*
@file      main.c
@brief     数据结构预备知识之跨函数使用内存
@author    EricsT (EricsT@163.com)
@version   v1.0.0
@date      2025-09-20
@history   2025-09-20 EricsT - 新建文件
*/#include <stdio.h>
#include <malloc.h>struct Student
{int sid;int age;
};Student* CreatStudent(void);
void ShowStudent(Student* ptrStu);int main(void)
{Student* ptrStu;//占4个字节//Student std;//占8个字节,所以采用指针操作ptrStu = CreatStudent();ptrStu->age = 10;ptrStu->sid = 99;ShowStudent(ptrStu);return 0;
}Student* CreatStudent(void)
{return (Student*)malloc(sizeof(Student));
}void ShowStudent(Student* ptrStu)
{printf("%d %d\n", ptrStu->age, ptrStu->sid);
}

 

http://www.hskmm.com/?act=detail&tid=11800

相关文章:

  • 2025.6第一套六级听力生词
  • CSP-S 2025游记
  • atof() - 字符串转double类型
  • 完整教程:还在为第三方包 bug 头疼?patch-package 让你轻松打补丁!
  • Kubernetes(k8s)高可用性集群的构建
  • 在CentOS环境下升级GCC编译器
  • 详细介绍:深圳比斯特|电池组PACK自动化生产线厂家概述
  • Chapter 4 Shapes and Texts
  • 手动清除Ubuntu系统中的内存缓存
  • 消除 Vue SPA 刷新导致 404 的问题
  • Docker / Kubernetes 图形化管理工具--------Portainer
  • 【Excel】创建下拉选项框
  • 不定高元素动画实现方案(中)
  • 技术文章
  • 插值相关
  • 密码学学习记录(三)
  • 详解scheduleAtFixedRate 与 scheduleWithFixedDelay 的区别
  • [题解]P11095 [ROI 2021] 旅行 (Day 2)
  • DDR5内存时序参数对照表
  • Linux CentOS 第三方扩展模块编译并安装
  • Java ArrayList中的常见删除操作及方法
  • ADC和GPIO的关系
  • 使用Docker Compose工具进行容器编排的教程
  • 模拟输入的过程
  • 基于Redisson和自定义注解的分布式锁实现策略
  • CCPC2025网络赛 游记
  • 知行合一
  • Manim实现水波纹特效
  • 深入解析:解锁AI智能体:上下文工程如何成为架构落地的“魔法钥匙”
  • JS之使用for...of赋值失败的原因分析