动态内存分配Dynamic allocation(C语言划重点)

Dynamic allocation of memory allows building complex data structures such as linked lists. Allocating memory dynamically helps us to store data without initially knowing the size of the data in the time we wrote the program.

To allocate a chunk of memory dynamically, we have to have a pointer ready - which will store the location of the newly allocated memory. We can access memory that was allocated to us using that same pointer, and we can use that pointer to free the memory we got, once we finish using it.

Let's assume we want to dynamically allocate a person structure. The person is defined like this:

typedef struct {char * name;int age;
} person;

To allocate a new person in the myperson argument, we use the following syntax:

person * myperson = malloc(sizeof(person));

This tells the compiler that we want to dynamically allocate just enough to hold a person struct in memory, and then return a pointer to the newly allocated data.

sizeof不是函数哦

Note that sizeof is not an actual function, because the compiler interprets it and translates it to the actual memory size of the person struct.

To access the person's members, we can use the -> notation:

myperson->name = "John";
myperson->age = 27;

After we are done using the dynamically allocated struct, we can release it using free:

free(myperson);

Note that the free does not delete the myperson variable itself, it simply releases the data that it points to. The myperson variable will still point to somewhere in the memory - but after calling myperson we are not allowed to access that area anymore. We must not use that pointer again until we allocate new data using it.

Exercise

Use malloc to dynamically allocate a point structure.

#include <stdio.h>
#include <stdlib.h>typedef struct {int x;int y;
} point;int main() {point * mypoint;mypoint = malloc(sizeof(point));mypoint->x = 10;mypoint->y =5 ;printf("mypoint coordinates: %d, %d\n", mypoint->x, mypoint->y);free(mypoint);return 0;
}

当定义一个结构体指针,下面分配内存赋值就不需要加*符号

动态内存分配Dynamic allocation(C语言划重点)相关推荐

  1. c语言动态内存分配数组,【C】动态内存分配

    ## 动态内存分配的意义 C语言中的一切操作都是基于内存的 变量和数组都是内存的别名 内存分配由编译器在编译期间决定 定义数组的时候必须指定数组长度 数组长度是在编译期就必须确定的需求: 程序在运行过 ...

  2. C/C++ 动态内存分配

    转自:https://blog.csdn.net/Code_beeps/article/details/89625473#comments 首先我们看看 c 语言是如何进行动态内存分配的: c 语言主 ...

  3. C语言之内存模型以及动态内存分配

    目录 内存分区模型 C/C++内存开辟 按照程序运行前后分区 程序运行前 代码区 全局区 程序运行后 栈区 堆区 关于栈 栈与静态区(数据段) 动态内存分配 那么使用动态内存的好处在哪? 常见的内存使 ...

  4. C语言动态内存分配详解

    文章目录 前言 一.为什么存在动态内存分配 1.已掌握的内存开辟方式 2.上述开辟空间方式的特点 3.为什么存在动态内存分配 二.动态内存函数的介绍 1.malloc 2.free 3.calloc ...

  5. 【C 语言】内存管理 ( 动态内存分配 | 栈 | 堆 | 静态存储区 | 内存布局 | 野指针 )

    相关文章链接 : 1.[嵌入式开发]C语言 指针数组 多维数组 2.[嵌入式开发]C语言 命令行参数 函数指针 gdb调试 3.[嵌入式开发]C语言 结构体相关 的 函数 指针 数组 4.[嵌入式开发 ...

  6. 在c语言中,可以使用动态内存分配技术定义元素个数可变的数组,C语言复制在线考题1精选.doc...

    C语言复制在线考题1精选 窗体顶端 <C语言程序设计208304>综合测试 返回测验列表 大项 1 / 2 - 单项选择题 60.0/ 70.0 分 本大题共35道,每题2分,共计70分, ...

  7. 【C语言进阶深度学习记录】三十三 C语言中动态内存分配

    如何在程序运行的时候动态给程序分配内存? 文章目录 1 动态内存分配的意义 1.1 C语言中如何动态申请内存空间 1.2 malloc和free的用法 1.3 calloc与realloc 1.31 ...

  8. c语言 malloc_C语言快速入门——动态内存分配

    在前面一系列的字符串操作中,我们都是先定义一个固定大小的字符数组,然后根据所需,或拷贝.或连接.或格式化来为这个数组提供内容.固定大小的数组意味着在程序运行期间,数组所占用的内存是确定的(即划分了固定 ...

  9. c语言链表内存分配失败,链表的C语言实现之动态内存分配

    链表的C语言实现之动态内存分配 來源:互聯網  2008-06-01 02:05:07  評論 一.为什么用动态内存分配 但我们未学习链表的时候,假如要存储数量比较多的同类型或同结构的数据的时候,总是 ...

最新文章

  1. 【并行计算-CUDA开发】从零开始学习OpenCL开发(一)架构
  2. 面向对象软件设计的“开—闭”原则
  3. 2013-10-31 《October 31st, 2013》
  4. 天翼云从业认证课后习题(3.5云安全产品)
  5. Enterprise Services (COM+)服务组件开发异常分析
  6. IDEA : 配置checkstyle
  7. API的 Signature(签名)Token(令牌) 认证
  8. 【论文写作】网上办公自动系统中功能需求如何写
  9. android 脚本发短信,Android使用SmsManager实现短信发送功能
  10. windows 空闲超时 非管理员如何破解
  11. 【转】Nutch源代码研究 网页抓取 数据结构
  12. xbox360自制系统服务器,没有想象那么难!XBOX360刷机详细教程
  13. c语言汇率兑换小程序,不懂算汇率?推荐你3个计算汇率的小程序,让你轻松算汇率...
  14. 白化滤波器 matlab,白化滤波器-matlab-程序.doc
  15. 如何使用IIS发布网站?
  16. 虚拟机安装教程(多图)
  17. 调用百度汇率api 获取各国的汇率值
  18. 【黑马程序员pink老师前端】HTML综合案例(二)
  19. 诚之和:被传估值150亿,元气森林凭什么?
  20. html按钮怎么正方形,css怎么画正方形?

热门文章

  1. 伪装成mysql的备_如何伪装成一个服务端开发(六) -- 数据库操作
  2. 【深度学习】保姆级教程:个人深度学习工作站配置指南
  3. ## java.lang.IllegalStateException:错误 修改
  4. 海豚湾在哪_菲律宾之海豚湾(PUERTO GALERA)攻略
  5. AutoCAD Civil 3D 介绍
  6. 英语面试自我介绍java_java面试英语自我介绍
  7. c语言帮助记忆单词的小程序,帮助记忆单词的书课堂活动微信小程序软件_速记背单词...
  8. Java 存牌洗牌发牌看牌
  9. python汇率转换代码_python 实现一个图形界面的汇率计算器
  10. 求证:原函数与逆函数具有相同的单调性