0
0
FreeRTOSprogramming~20 mins

pvPortMalloc and vPortFree in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
FreeRTOS Memory Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this FreeRTOS memory allocation code?

Consider this FreeRTOS snippet using pvPortMalloc and vPortFree. What will be printed?

FreeRTOS
void *ptr = pvPortMalloc(100);
if(ptr != NULL) {
    printf("Allocated 100 bytes\n");
    vPortFree(ptr);
    printf("Memory freed\n");
} else {
    printf("Allocation failed\n");
}
A
Allocated 100 bytes
Memory freed
BAllocation failed
CAllocated 100 bytes
DMemory freed
Attempts:
2 left
💡 Hint

Think about what happens if pvPortMalloc returns a valid pointer.

🧠 Conceptual
intermediate
1:30remaining
What happens if you call vPortFree with a NULL pointer?

In FreeRTOS, what is the expected behavior when vPortFree(NULL) is called?

AIt frees the entire heap memory
BIt causes a runtime error and crashes the system
CIt safely does nothing and returns immediately
DIt allocates new memory instead
Attempts:
2 left
💡 Hint

Think about how standard C free(NULL) behaves.

🔧 Debug
advanced
2:30remaining
Why does this FreeRTOS code cause a heap corruption error?

Analyze the code below. Why might it cause heap corruption?

FreeRTOS
void *ptr = pvPortMalloc(50);
// ... some code ...
vPortFree(ptr);
vPortFree(ptr);
ApvPortMalloc does not allocate memory properly
BDouble free of the same pointer causes heap corruption
CvPortFree requires size parameter which is missing
DFreeing memory twice is allowed and safe
Attempts:
2 left
💡 Hint

What happens if you free the same memory twice?

📝 Syntax
advanced
2:00remaining
Which option correctly allocates and frees memory in FreeRTOS?

Choose the code snippet that correctly uses pvPortMalloc and vPortFree without syntax errors.

A
char *buf = pvPortMalloc(128);
if(buf) {
  vPortFree(buf);
}
B
char *buf = pvPortMalloc(128)
if(buf) {
  vPortFree(buf)
}
C
char *buf = pvPortMalloc(128);
if(buf) {
  vPortFree();
}
D
char *buf = pvPortMalloc(128);
if(buf) {
  // use buf
  vPortFree(buf);
}
Attempts:
2 left
💡 Hint

Check for missing semicolons and correct function calls.

🚀 Application
expert
3:00remaining
How many items are in the heap after this sequence of allocations and frees?

Given the following FreeRTOS code, how many allocated blocks remain in the heap?

FreeRTOS
void *p1 = pvPortMalloc(20);
void *p2 = pvPortMalloc(30);
vPortFree(p1);
void *p3 = pvPortMalloc(40);
vPortFree(p2);
A1
B2
C3
D0
Attempts:
2 left
💡 Hint

Count how many pointers are still allocated and not freed.