Bird
0
0

Given the following code using heap_2:

medium📝 Predict Output Q4 of 15
FreeRTOS - Memory Management
Given the following code using heap_2:
void *ptr1 = pvPortMalloc(100);
void *ptr2 = pvPortMalloc(200);
pvPortFree(ptr1);
void *ptr3 = pvPortMalloc(50);
What is the expected behavior of ptr3 allocation?
Aptr3 will point to the same address as ptr2
Bptr3 will reuse the freed 100-byte block from ptr1
Cptr3 allocation will fail due to fragmentation
Dptr3 will allocate a new block without reusing freed memory
Step-by-Step Solution
Solution:
  1. Step 1: Understand heap_2 behavior

    heap_2 supports freeing memory and reuses freed blocks, but does not coalesce adjacent free blocks.
  2. Step 2: Analyze allocation of ptr3

    ptr1's 100-byte block is freed and added to the free list. ptr3 (50 bytes) fits in it, so ptr3 reuses the freed block from ptr1.
  3. Final Answer:

    ptr3 will reuse the freed 100-byte block from ptr1 -> Option B
  4. Quick Check:

    heap_2 reuses freed blocks [OK]
Quick Trick: heap_2 reuses freed blocks but does not coalesce [OK]
Common Mistakes:
  • Thinking heap_2 does not reuse freed blocks
  • Thinking ptr3 allocation fails due to fragmentation
  • Confusing ptr3 address with ptr2

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FreeRTOS Quizzes