Consider FreeRTOS heap_1 implementation which provides pvPortMalloc() and vPortFree(). What is the output of the following pseudo-code snippet?
void *p1 = pvPortMalloc(100);
pvPortFree(p1);
void *p2 = pvPortMalloc(100);
if (p1 == p2) printf("Same pointer reused\n"); else printf("Different pointer\n");Assuming heap_1 is used and memory is sufficient.
void *p1 = pvPortMalloc(100); pvPortFree(p1); void *p2 = pvPortMalloc(100); if (p1 == p2) printf("Same pointer reused\n"); else printf("Different pointer\n");
heap_1 does not implement vPortFree(), so freeing does nothing.
heap_1 provides a very simple allocator that does not actually free memory. So the pointer returned by the second malloc is the same as the first.
Using FreeRTOS heap_4 implementation, what will be the output of this sequence?
void *p1 = pvPortMalloc(50);
void *p2 = pvPortMalloc(100);
pvPortFree(p1);
void *p3 = pvPortMalloc(40);
if (p3 == p1) printf("Reused freed block\n"); else printf("Allocated new block\n");void *p1 = pvPortMalloc(50); void *p2 = pvPortMalloc(100); pvPortFree(p1); void *p3 = pvPortMalloc(40); if (p3 == p1) printf("Reused freed block\n"); else printf("Allocated new block\n");
heap_4 supports coalescing and reusing freed blocks.
heap_4 manages free blocks and will reuse the freed block if the requested size fits, so p3 equals p1.
Which statement correctly describes the difference between FreeRTOS heap_2 and heap_3 implementations?
heap_3 wraps standard library malloc/free.
heap_2 supports freeing memory but is not thread-safe. heap_3 uses the standard malloc/free from the C library and is thread-safe if the standard library is thread-safe.
Given heap_5 configured with two memory regions, what will be the output of this code?
void *p1 = pvPortMalloc(1024);
void *p2 = pvPortMalloc(2048);
if (p1 != NULL && p2 != NULL) printf("Both allocations succeeded\n"); else printf("Allocation failed\n");Assume region 1 has 1024 bytes and region 2 has 2048 bytes.
void *p1 = pvPortMalloc(1024); void *p2 = pvPortMalloc(2048); if (p1 != NULL && p2 != NULL) printf("Both allocations succeeded\n"); else printf("Allocation failed\n");
heap_5 supports multiple memory regions.
heap_5 can allocate from multiple regions independently, so both allocations succeed.
Analyze the output of this heap_4 memory allocation and free sequence:
void *p1 = pvPortMalloc(100);
void *p2 = pvPortMalloc(200);
void *p3 = pvPortMalloc(50);
pvPortFree(p2);
pvPortFree(p1);
void *p4 = pvPortMalloc(250);
if (p4 == p1) printf("Coalesced free blocks used\n"); else printf("Allocated new block\n");void *p1 = pvPortMalloc(100); void *p2 = pvPortMalloc(200); void *p3 = pvPortMalloc(50); pvPortFree(p2); pvPortFree(p1); void *p4 = pvPortMalloc(250); if (p4 == p1) printf("Coalesced free blocks used\n"); else printf("Allocated new block\n");
heap_4 merges adjacent free blocks to satisfy larger allocations.
heap_4 coalesces adjacent free blocks, so the freed blocks of 100 and 200 bytes merge to satisfy the 250-byte allocation, returning the pointer of the first freed block.