Consider this FreeRTOS snippet using pvPortMalloc and vPortFree. What will be printed?
void *ptr = pvPortMalloc(100); if(ptr != NULL) { printf("Allocated 100 bytes\n"); vPortFree(ptr); printf("Memory freed\n"); } else { printf("Allocation failed\n"); }
Think about what happens if pvPortMalloc returns a valid pointer.
If pvPortMalloc succeeds, it returns a non-NULL pointer, so both messages print. Otherwise, only the failure message prints.
In FreeRTOS, what is the expected behavior when vPortFree(NULL) is called?
Think about how standard C free(NULL) behaves.
Calling vPortFree(NULL) is safe and does nothing, similar to standard C free(NULL).
Analyze the code below. Why might it cause heap corruption?
void *ptr = pvPortMalloc(50);
// ... some code ...
vPortFree(ptr);
vPortFree(ptr);What happens if you free the same memory twice?
Calling vPortFree twice on the same pointer causes heap corruption because the memory is freed twice.
Choose the code snippet that correctly uses pvPortMalloc and vPortFree without syntax errors.
Check for missing semicolons and correct function calls.
Option D has correct syntax with semicolons and proper vPortFree call. Option D misses semicolons, A is syntactically correct, D calls vPortFree without argument.
Given the following FreeRTOS code, how many allocated blocks remain in the heap?
void *p1 = pvPortMalloc(20); void *p2 = pvPortMalloc(30); vPortFree(p1); void *p3 = pvPortMalloc(40); vPortFree(p2);
Count how many pointers are still allocated and not freed.
p1 and p2 are freed, p3 is allocated and not freed, so only one block remains allocated.