zoukankan      html  css  js  c++  java
  • [RTT例程练习] 3.1 动态内存管理之rt_malloc和rt_free

    rt_malloc和rt_free 和 malloc free 类似,是用来在堆上分配内存的,RT-Thread中是用小内存法来实现的。下面程序就是不停分配内存和释放内存,并打印分配到的内存首地址。

    程序:

    #include <rtthread.h>
    
    struct rt_thread thread1;
    static rt_uint8_t thread1_stack[512];
    static void thread1_entry(void *parameter)
    {
        int i,j;
        char *ptr[20];
        
        for (j = 0; j < 20; j++)
            ptr[j] = RT_NULL;
        
        for (i = 0; i < 2; i++)
        {
            for (j = 0; j < 20; j++)
            {
                ptr[j] = rt_malloc(1 << j);
                
                if (ptr[j] != RT_NULL)
                {
                    rt_kprintf("get memory: 0x%x.\n", ptr[j]);
                    
                    rt_free(ptr[j]);
                    ptr[j] = RT_NULL;
                }
            }
        }
    }
    
    int rt_application_init()
    {
        rt_thread_init(&thread1,
            "thread1",
            thread1_entry, RT_NULL,
            &thread1_stack[0], sizeof(thread1_stack),
            10, 100);
        rt_thread_startup(&thread1);
        
        return 0;
    }

    结果:

    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58
    get memory: 0x20000c58


  • 相关阅读:
    DDOS学习笔记(《破坏之王-DDOS攻击与防范深度剖析》)
    gearman学习笔记1
    Sphinx学习笔记2
    docker学习笔记(一)
    Centos7安装配置Xhgui
    MongodDB学习笔记(二)(复制)
    MongoDB学习笔记(一)
    0927 DP 小测 #1
    「NOI 2011」阿狸的打字机 「AC 自动机」「数据结构」
    「POI 2005」SZA-Template 「失配树」「双向链表」「思维」
  • 原文地址:https://www.cnblogs.com/lyyyuna/p/4123920.html
Copyright © 2011-2022 走看看