Bird
Raised Fist0
FreeRTOSprogramming~20 mins

pvPortMalloc and vPortFree in FreeRTOS - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the primary purpose of pvPortMalloc in FreeRTOS?
easy
A. To suspend a running task temporarily
B. To free allocated memory back to the system
C. To create a new task in FreeRTOS
D. To allocate memory safely for tasks and resources

Solution

  1. Step 1: Understand the role of pvPortMalloc

    pvPortMalloc is designed to allocate memory safely in FreeRTOS, especially in multitasking environments.
  2. Step 2: Differentiate from other functions

    Functions like vPortFree free memory, and task creation or suspension are unrelated to memory allocation.
  3. Final Answer:

    To allocate memory safely for tasks and resources -> Option D
  4. Quick Check:

    pvPortMalloc = allocate memory [OK]
Hint: pvPortMalloc always allocates memory safely [OK]
Common Mistakes:
  • Confusing pvPortMalloc with vPortFree
  • Thinking pvPortMalloc creates tasks
  • Assuming it suspends tasks
2. Which of the following is the correct syntax to free memory allocated by pvPortMalloc?
easy
A. vPortFree(pointer);
B. pvPortMalloc(pointer);
C. free(pointer);
D. vPortMalloc(pointer);

Solution

  1. Step 1: Identify the function to free memory

    Memory allocated by pvPortMalloc must be freed using vPortFree to avoid memory leaks.
  2. Step 2: Check syntax correctness

    The correct syntax is vPortFree(pointer);. Other options either allocate memory or use standard C free, which is not recommended in FreeRTOS.
  3. Final Answer:

    vPortFree(pointer); -> Option A
  4. Quick Check:

    Free memory with vPortFree [OK]
Hint: Use vPortFree to release memory allocated by pvPortMalloc [OK]
Common Mistakes:
  • Using free() instead of vPortFree()
  • Calling pvPortMalloc to free memory
  • Incorrect function names like vPortMalloc
3. Consider the following FreeRTOS code snippet:
void *ptr = pvPortMalloc(100);
if(ptr != NULL) {
    vPortFree(ptr);
    ptr = NULL;
}
printf("%p", ptr);

What will be the output of the printf statement?
medium
A. A memory address (non-NULL pointer)
B. Compilation error
C. NULL (0x0 or equivalent)
D. Random garbage value

Solution

  1. Step 1: Analyze memory allocation and freeing

    pvPortMalloc(100) allocates memory and returns a pointer. Then vPortFree(ptr) frees it, and ptr is set to NULL.
  2. Step 2: Understand the printf output

    Since ptr is explicitly set to NULL, printf will print 0x0 or equivalent NULL pointer representation.
  3. Final Answer:

    NULL (0x0 or equivalent) -> Option C
  4. Quick Check:

    Pointer set to NULL after free = NULL output [OK]
Hint: Pointer set to NULL after free prints NULL [OK]
Common Mistakes:
  • Assuming pointer still holds address after free
  • Expecting compilation error due to free
  • Thinking printf prints garbage
4. Identify the error in the following FreeRTOS code snippet:
void *ptr = pvPortMalloc(50);
// Some code using ptr
vPortFree(ptr);
vPortFree(ptr);
medium
A. Double free of the same pointer
B. Missing check if pvPortMalloc returned NULL
C. Incorrect function name vPortFree
D. Memory leak due to not freeing ptr

Solution

  1. Step 1: Review memory allocation and freeing

    pvPortMalloc(50) allocates memory and returns a pointer stored in ptr. The first vPortFree(ptr) frees this memory.
  2. Step 2: Identify the problem with second free

    The second vPortFree(ptr) attempts to free the same memory again, causing a double free error which can crash or corrupt memory.
  3. Final Answer:

    Double free of the same pointer -> Option A
  4. Quick Check:

    Freeing pointer twice causes error [OK]
Hint: Never free the same pointer twice without resetting it [OK]
Common Mistakes:
  • Ignoring double free risks
  • Not checking for NULL after malloc
  • Confusing function names
5. You want to allocate memory for an array of 10 integers using FreeRTOS memory management. Which code snippet correctly allocates and frees this memory?
hard
A. int *arr = (int *)pvPortMalloc(sizeof(int)); if(arr != NULL) { // use arr vPortFree(arr); }
B. int *arr = (int *)pvPortMalloc(10 * sizeof(int)); if(arr != NULL) { // use arr vPortFree(arr); }
C. int *arr = (int *)malloc(10 * sizeof(int)); if(arr != NULL) { // use arr free(arr); }
D. int *arr = (int *)vPortFree(10 * sizeof(int)); if(arr != NULL) { // use arr pvPortMalloc(arr); }

Solution

  1. Step 1: Allocate correct memory size with pvPortMalloc

    To store 10 integers, allocate 10 times the size of int using pvPortMalloc(10 * sizeof(int)). This ensures enough space.
  2. Step 2: Free memory properly with vPortFree

    After using the array, free the memory with vPortFree(arr). Also, check if allocation succeeded by verifying arr != NULL.
  3. Final Answer:

    Correct allocation and freeing using pvPortMalloc and vPortFree -> Option B
  4. Quick Check:

    Allocate 10 ints with pvPortMalloc, free with vPortFree [OK]
Hint: Multiply size by count in pvPortMalloc, free with vPortFree [OK]
Common Mistakes:
  • Allocating only size of one int instead of 10
  • Using standard malloc/free instead of pvPortMalloc/vPortFree
  • Confusing allocation and free function calls
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard