当前位置:网站首页>C陷阱与缺陷 第7章 可移植性缺陷 7.10 首先释放,然后重新分配
C陷阱与缺陷 第7章 可移植性缺陷 7.10 首先释放,然后重新分配
2022-08-01 21:00:00 【weixin_客子光阴】
首先释放,然后重新分配
C语言实现都为使用人员提供了3个内存分配函数:malloc、realloc和free。malloc(n)将返回一个指针,指向一块新分配的可以容纳n的字符的内存,编程人员可以使用这块内存。free将释放malloc分配的内存,方式是通过把malloc函数返回的指针作为参数传递给free函数。realloc需要把指向一块内存已分配内存的区域指针以及这块内存新的大小作为参数传入,就可以调整(扩大或缩小)这块内存区域为新的大小,这个过程中有可能涉及内存的复制。
内存重分配(reallocation)
这一实现允许某内存块被释放之后重新分配其大小,前提是内存重分配(reallocation)操作执行得必须足够早。(UNIX)
例如,下面的代码就是合法的:
free(p);
p = realloc(p, newsize);
释放一个链表中的所有元素:
for (p = head; p != NULL; p = p->next) {
free((char *)p)
}
这里,我们不必担心调用free之后,会使p->next变得无效当着,这种技巧不值得推荐。(有可能在其它系统上失败)
边栏推荐
- WeChat applet cloud development | personal blog applet
- tiup mirror
- 关于Request复用的那点破事儿。研究明白了,给你汇报一下。
- Zheng Xiangling, Chairman of Tide Pharmaceuticals, won the "2022 Outstanding Influential Entrepreneur Award" Tide Pharmaceuticals won the "Corporate Social Responsibility Model Award"
- 自定义指令,获取焦点
- C专家编程 第1章 C:穿越时空的迷雾 1.5 今日之ANSI C
- LinkedList source code sharing
- 移植MQTT源码到STM32F407开发板上
- [Personal work] Wireless network image transmission module
- Excel advanced drawing techniques, 100 (22) - how to respectively the irregular data
猜你喜欢
idea插件generateAllSetMethod一键生成set/get方法以及bean对象转换
用户身份标识与账号体系实践
Application of Acrel-5010 online monitoring system for key energy consumption unit energy consumption in Hunan Sanli Group
[Energy Conservation Institute] Application of Intelligent Control Device in High Voltage Switchgear
StringTable Detailed String Pool Performance Tuning String Concatenation
Where should I prepare for the PMP exam in September?
98.嵌入式控制器EC实战 EC开发板开发完成
响应式织梦模板清洁服务类网站
漏洞分析丨HEVD-0x6.UninitializedStackVariable[win7x86]
PyTorch笔记 - Attention Is All You Need (2)
随机推荐
写给刚进互联网圈子的人,不管你是开发,测试,产品,运维都适用
面试官:大量请求 Redis 不存在的数据,从而打倒数据库,有什么方案?
Multithreaded producers and consumers
通过这两个 hook 回顾 Set/Map 基础知识
MySQL Syntax Basics
useful website
那些关于DOM的常见Hook封装(二)
Godaddy域名解析速度慢问题以及如何使用DNSPod解析解决
STAHL触摸屏维修一体机显示屏ET-316-TX-TFT常见故障
Telnet弱口令渗透测试
idea实用快捷键合集——持续更新
JS提升:如何中断Promise的链式调用
响应式织梦模板美容整形类网站
[Energy Conservation Institute] Ankerui Food and Beverage Fume Monitoring Cloud Platform Helps Fight Air Pollution
线上问题排查常用命令,总结太全了,建议收藏!!
数据库内核面试中我不会的问题(1)
string
Use WeChat official account to send information to designated WeChat users
乐观锁批量跟新 纯SQL
使用员工管理软件,解锁人力生产力新水平,提高人力资源团队灵活性