Keil MDK 使用malloc()&free(),stm32简单测试可用。
2015-12-08 18:06
936 查看
1.8.9 Using malloc() when exploiting the C library
If heap support is required for bare machine C, you must implement _init_alloc()and
__rt_heap_extend().
_init_alloc()must be called first to supply initial heap bounds, and
__rt_heap_extend()must
be provided even if it only returns failure. Without
__rt_heap_extend(), certain library functionality is included that causes
problems when you are writing bare machine C.
Prototypes for both
_init_alloc()and
__rt_heap_extend()are
in rt_heap.h.
以上摘自http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.subset.swdev.comp6/index.html
程序包含头文件:rt_heap.h,stdlib.h。注意不要勾选MicroLIB。
汇编代码中设置Heap大小,我设置为4KB。
Heap_Size EQU 0x00004000
跟高级的方法如下:
简单的测试代码如下:
#define HEAP_BASE 0x20002558 #define HEAP_SIZE 0x00004000 #define HEAP_END HEAP_BASE+HEAP_SIZE void testmem() { int *p,*k=NULL; int a=0; volatile int i=0; _init_alloc(HEAP_BASE,HEAP_END); while(1) { if(a>1000)break; p=(int*)malloc(a++); i=(unsigned int)p-(unsigned int)k; k=p; *p=a; //free(p); } }调试可看到如下结果:
其中p,k是两次malloc得到的地址,i为两次得到的mallac得到地址的间隔,需要注意malloc得到的内存是8字节对其的。a是写入的一个数据。该程序执行一段时间后会内存泄漏,因为没free,去掉free()的注释即可。以下是free(p)的调试结果:经过多次malloc后得到的内存地址始终是0x20002570。
总结:
.If heap support is required for bare machine C, you must implement
_init_alloc()and
__rt_heap_extend().
分配的内存地址8byte对齐;
#define HEAP_BASE 0x20002558这里的地址可以看*.map文件Heap_Mem的值。这里应该可以通过汇编和C的混合编程来实现,暂时没试。
饿死了,以上暂作笔记用,找时间再整理下测试下。
相关文章推荐
- android 开启调试小技巧
- sql: sql developer tunnel转接
- Data Vault初探(三) —— 建立Data Vault模型
- 数据结构-字符串
- mac安装ubuntu
- FUSE源码剖析
- 问题解决记录 - KRBD Map失败
- SQLServer2014新功能
- iOS自定义tabbar后popToRootViewContriller和poptoviewcontroller时出现两个tabbar 的解决办法
- python中,一个函数想使用另一个函数中的变量
- java 发送http请求
- DNS域名解析服务
- 关于Eclipse 自动产生appcompat_v7
- php无限极分类
- ZZULIOJ 1804 ZY学长的密码
- 【Java并发编程实战】—–Unsafe类
- HNOI2013 比赛
- CentOS 7 NFS服务器和客户端设置
- SpringMVC学习笔记001
- 实习day2:@2X图片,git,coding.net,