[RTT 루틴 연습] 3.1 동적 메모리 관리 rtmalloc 및 rtfree

1879 단어 rtt
rt_malloc 및 rtfree는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.
", 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

좋은 웹페이지 즐겨찾기