当前位置:网站首页>C pitfalls and pitfalls Chapter 7. Portability pitfalls 7.10 Free first, then realloc
C pitfalls and pitfalls Chapter 7. Portability pitfalls 7.10 Free first, then realloc
2022-08-01 21:04:00 【Weixin_ guest child time】
First release, then reallocate
The C language implementation provides three memory allocation functions for users: malloc, realloc and free.malloc(n) will return a pointer to a newly allocated block of memory that can hold characters of n, which the programmer can use.free will free the memory allocated by malloc by passing the pointer returned by the malloc function as an argument to the free function.realloc needs to pass in the pointer to the allocated memory area of a block of memory and the new size of the memory as parameters, so that the memory area can be adjusted (expanded or reduced) to the new size. This process may involve memory copying.
memory reallocation (reallocation)
This implementation allows a block of memory to be reallocated after it is freed, provided that the memory reallocation operation is performed early enough.(UNIX)
For example, the following code is legal:
free(p);
p = realloc(p, newsize);
Free all elements in a linked list:
for(p = head; p != NULL; p=p->next) {
free((char *)p)
}
Here, we don't have to worry about calling free, which will make p->next invalid.Recommended.(may fail on other systems)
边栏推荐
- Pytorch框架学习记录9——非线性激活
- C专家编程 第1章 C:穿越时空的迷雾 1.1 C语言的史前阶段
- C陷阱与缺陷 第7章 可移植性缺陷 7.7 除法运算时发生的截断
- 98. Embedded controller EC actual combat EC development board development completed
- Goroutine Leaks - The Forgotten Sender
- 响应式织梦模板清洁服务类网站
- 分类接口,淘宝分类详情 API
- 线上一次JVM FullGC搞得整晚都没睡,彻底崩溃~
- 织梦发布文章提示body has not allow words错误
- 有点奇怪!访问目的网址,主机能容器却不行
猜你喜欢
随机推荐
Postman 批量测试接口详细教程
密码学的基础:X.690和对应的BER CER DER编码
数据库单字段存储多个标签(位移操作)
STAHL touch screen repair all-in-one display screen ET-316-TX-TFT common faults
Pytorch框架学习记录10——线性层
【接口测试】JMeter调用JS文件实现RSA加密
idea插件generateAllSetMethod一键生成set/get方法以及bean对象转换
tiup mirror genkey
[译] 容器和 Kubernetes 中的退出码完整指南
idea实用快捷键合集——持续更新
Pytorch框架学习记录9——非线性激活
Based on FPGA in any number of bytes (single-byte or multibyte) serial port (UART) to send (including source engineering)
写给刚进互联网圈子的人,不管你是开发,测试,产品,运维都适用
【Kaggle】House Prices
那些关于DOM的常见Hook封装(二)
相亲模型与有限状态机
漏洞分析丨HEVD-0x6.UninitializedStackVariable[win7x86]
tiup mirror
函数(二)
使用员工管理软件,解锁人力生产力新水平,提高人力资源团队灵活性