Bird
0
0

Given the following code snippet using heap_4, what will be the output if the allocation fails?

medium📝 Predict Output Q13 of 15
FreeRTOS - Memory Management
Given the following code snippet using heap_4, what will be the output if the allocation fails?
void *ptr = pvPortMalloc(1000);
if(ptr == NULL) {
    printf("Allocation failed\n");
} else {
    printf("Allocation succeeded\n");
}
ACompilation error
BAllocation succeeded
CAllocation failed
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Understand heap_4 allocation behavior

    heap_4 supports allocation and freeing with coalescing. If memory is insufficient, pvPortMalloc returns NULL.
  2. Step 2: Analyze the if condition

    If ptr is NULL, the code prints "Allocation failed"; otherwise, it prints "Allocation succeeded".
  3. Final Answer:

    Allocation failed -> Option C
  4. Quick Check:

    pvPortMalloc NULL means failure = A [OK]
Quick Trick: pvPortMalloc returns NULL on failure [OK]
Common Mistakes:
  • Assuming allocation always succeeds
  • Confusing NULL with zero pointer
  • Ignoring the if condition logic

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FreeRTOS Quizzes