Bird
0
0

You need to allocate memory for an array of 10 integers in FreeRTOS using pvPortMalloc. Which of the following code snippets correctly performs this allocation and deallocation?

hard📝 Application Q8 of 15
FreeRTOS - Memory Management
You need to allocate memory for an array of 10 integers in FreeRTOS using pvPortMalloc. Which of the following code snippets correctly performs this allocation and deallocation?
Aint *arr = (int *)pvPortMalloc(10 * sizeof(int)); if(arr != NULL) { // use arr vPortFree(arr); }
Bint *arr = (int *)pvPortMalloc(sizeof(int)); if(arr != NULL) { // use arr vPortFree(arr); }
Cint *arr = (int *)malloc(10 * sizeof(int)); if(arr != NULL) { // use arr free(arr); }
Dint *arr = (int *)pvPortMalloc(10); if(arr != NULL) { // use arr vPortFree(arr); }
Step-by-Step Solution
Solution:
  1. Step 1: Calculate the correct size

    To allocate memory for 10 integers, multiply 10 by sizeof(int) to get the total bytes needed.
  2. Step 2: Use pvPortMalloc for allocation

    Use pvPortMalloc with the calculated size and cast the returned void pointer to int *.
  3. Step 3: Free the allocated memory

    After usage, free the memory using vPortFree to avoid memory leaks.
  4. Final Answer:

    int *arr = (int *)pvPortMalloc(10 * sizeof(int)); if(arr != NULL) { // use arr vPortFree(arr); } correctly allocates and frees the memory.
  5. Quick Check:

    Allocate size = 10 * sizeof(int), free with vPortFree [OK]
Quick Trick: Multiply count by sizeof(type) for allocation [OK]
Common Mistakes:
  • Allocating only sizeof(int) bytes instead of 10 times
  • Using standard malloc/free instead of pvPortMalloc/vPortFree
  • Allocating 10 bytes instead of 10 integers

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FreeRTOS Quizzes