0
0
FreeRTOSprogramming~20 mins

FreeRTOS heap implementations (heap_1 to heap_5) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FreeRTOS Heap Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of heap_1 malloc and free behavior

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.

FreeRTOS
void *p1 = pvPortMalloc(100);
pvPortFree(p1);
void *p2 = pvPortMalloc(100);
if (p1 == p2) printf("Same pointer reused\n"); else printf("Different pointer\n");
ARuntime error due to double free
BDifferent pointer
CCompilation error due to missing free implementation
DSame pointer reused
Attempts:
2 left
💡 Hint

heap_1 does not implement vPortFree(), so freeing does nothing.

Predict Output
intermediate
2:00remaining
heap_4 behavior with fragmentation

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");
FreeRTOS
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");
AAllocated new block
BReused freed block
CHeap corruption detected
DMemory leak warning
Attempts:
2 left
💡 Hint

heap_4 supports coalescing and reusing freed blocks.

🧠 Conceptual
advanced
2:00remaining
Difference between heap_2 and heap_3

Which statement correctly describes the difference between FreeRTOS heap_2 and heap_3 implementations?

Aheap_2 supports freeing memory but is not thread-safe; heap_3 uses standard malloc/free and is thread-safe
Bheap_2 does not support freeing memory; heap_3 supports freeing memory but is not thread-safe
Cheap_2 supports freeing memory and is not thread-safe; heap_3 uses standard malloc/free and is thread-safe
Dheap_2 uses standard malloc/free; heap_3 is a simple bump allocator without free
Attempts:
2 left
💡 Hint

heap_3 wraps standard library malloc/free.

Predict Output
advanced
2:00remaining
heap_5 multi-region allocation behavior

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.

FreeRTOS
void *p1 = pvPortMalloc(1024);
void *p2 = pvPortMalloc(2048);
if (p1 != NULL && p2 != NULL) printf("Both allocations succeeded\n"); else printf("Allocation failed\n");
ABoth allocations succeeded
BAllocation failed
COnly second allocation succeeded
DOnly first allocation succeeded
Attempts:
2 left
💡 Hint

heap_5 supports multiple memory regions.

Predict Output
expert
3:00remaining
heap_4 fragmentation and coalescing test

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");
FreeRTOS
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");
ACoalesced free blocks used
BAllocated new block
CHeap corruption error
DMemory leak detected
Attempts:
2 left
💡 Hint

heap_4 merges adjacent free blocks to satisfy larger allocations.