MDK 使用 malloc free

9 篇文章 1 订阅

(1)栈区(stack):由编译器自动分配和释放,存放函数的参数值、局部变量的值等,其操作方式类似于数据结构中的栈。

(2)堆区(heap):一般由程序员分配和释放,若程序员不释放,程序结束时可能由操作系统回收。分配方式类似于数据结构中的链表。

(3)全局区(静态区)(static):全局变量和静态变量的存储是放在一块的,初始化的全局变量和静态变量在一块区域,未初始化的全局变量和未初始化的静态变量在相邻的另一块区域。程序结束后由系统自动释放。

(4)文字常量区:常量字符串就是存放在这里的。

(5)程序代码区:存放函数体的二进制代码。

一、使用 MicroLIB https://blog.csdn.net/electrocrazy/article/details/76241705

直接勾选“ Use MicroLIB ”,则在程序编译时,编译器自动加载  MicroLIB!
(1)MicroLIB和C标准库互斥,两者不能同时使用,只能2选1;
(2) 使用MicroLIB后就可以直接使用printf()往串口终端打印信息了,默认使用串口1;
(3) MicroLIB提供了一个有限的 stdio子系统,它仅支持未缓冲的 stdin、stdout和stderr。要使用高级 I/O 函数,必须自己对fputc()、fgetc()和__backspace()基本函数进行修改;
(4)MicroLIB中不支持的转换为 %lc、%ls和 %a;
(5) MicroLIB不支持与操作系统交互的所有函数,例如:abort()、exit()、atexit()、clock()、time()、system()和getenv();
 
/
//USE  MicroLIB
//void *malloc(unsigned int num_bytes);
//--------------------------------------------------
//The Head Will Use EIGHT Bytes.
//The Mark Will Use FOUR Bytes and The AutoAdlign Is
//Also FOUR Bytes. The Exp as Follow.
//--------------------------------------------------
//HEAP BASE:0x200135c8
//      pHeapA : 0x200135D0
//      pHeapB : 0x200135D8
//      pHeapC : 0x200135F0
//IF the HEAP Space Is Used almost Over The Reault
//of "malloc" will be NULL.
// -------------------------------------------------
// We Can Importing a linker-defined symbol.
/
extern unsigned int Stack_Size;
unsigned int StackSize = (unsigned int)&Stack_Size;
extern unsigned int __initial_sp;
unsigned int StackBase = (unsigned int)&__initial_sp;
vm_printf("StackSize = 0x%x\n", StackSize);
vm_printf("StackBase = 0x%x\n\n", StackBase);
 
extern unsigned int Heap_Size;
unsigned int HeapSize = (unsigned int)&Heap_Size;
extern unsigned int __heap_base;
unsigned int HeapBase = (unsigned int)&__heap_base;
extern unsigned int __heap_limit;
unsigned int HeapLimit = (unsigned int)&__heap_limit;
vm_printf("HeapSize = 0x%x\n", HeapSize);
vm_printf("HeapBase = 0x%x\n", HeapBase);
vm_printf("HeapLimit = 0x%x\n\n", HeapLimit);
 
U8 *pHeapA = malloc(0x01);
U8 *pHeapB = malloc(0x13);
U8 *pHeapC = malloc(0x01);
printf("pHeapA : 0x%x\n", pHeapA);
printf("pHeapB : 0x%x\n", pHeapB);
printf("pHeapC : 0x%x\n", pHeapC);
free(pHeapB);
pHeapB = malloc(0x10);
printf("pHeapB : 0x%x\n", pHeapB);
free(pHeapB);
pHeapB = malloc(0x20);
printf("pHeapB : 0x%x\n", pHeapB);

 

二、使用标准库

// Use Stand C LIB
//------------------------------------------------------
// The Head Will Use THIRTY_TWO Bytes.
// The MARK: If It can Align By FOUR Bytes with
// Its Last Region It will Use FOUR, Else If It
// Will Align As EIGHT Bytes From 0xXXX0.
// Please Do Not Implement __user_initial_stackheap
#if !__MICROLIB
#include<rt_heap.h>
#include<stdlib.h>
#pragma import (__use_realtime_heap)
//
//  r0 is the minimum size of the block to add.
// r1 holds a pointer to a location to store the base address.
//
unsigned int __rt_heap_extend(unsigned size, void **block)
{
    return 0;
}
_init_alloc(HeapBase, HeapLimit);
#endif
 
extern unsigned int Stack_Size;
unsigned int StackSize = (unsigned int)&Stack_Size;
extern unsigned int __initial_sp;
unsigned int StackBase = (unsigned int)&__initial_sp;
vm_printf("StackSize = 0x%x\n", StackSize);
vm_printf("StackBase = 0x%x\n\n", StackBase);
 
extern unsigned int Heap_Size;
unsigned int HeapSize = (unsigned int)&Heap_Size;
extern unsigned int __heap_base;
unsigned int HeapBase = (unsigned int)&__heap_base;
extern unsigned int __heap_limit;
unsigned int HeapLimit = (unsigned int)&__heap_limit;
vm_printf("HeapSize = 0x%x\n", HeapSize);
vm_printf("HeapBase = 0x%x\n", HeapBase);
vm_printf("HeapLimit = 0x%x\n\n", HeapLimit);
 
U8 *pHeapA = malloc(0x01);
U8 *pHeapB = malloc(0x0C);
U8 *pHeapC = malloc(0x0D);
U8 *pHeapD = malloc(0x11);
 
 
vm_printf("pHeapA : 0x%x\n", pHeapA);
vm_printf("pHeapB : 0x%x\n", pHeapB);
vm_printf("pHeapC : 0x%x\n", pHeapC);
vm_printf("pHeapD : 0x%x\n", pHeapD);
free(pHeapB);
pHeapB = malloc(0x10);
vm_printf("pHeapB : 0x%x\n", pHeapB);
free(pHeapB);
pHeapB = malloc(0x20);
vm_printf("pHeapB : 0x%x\n", pHeapB);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值