Bird
0
0

In a FreeRTOS application, you want to allocate memory for a struct and initialize it immediately. Which code correctly combines pvPortMalloc and initialization?

hard📝 Application Q9 of 15
FreeRTOS - Memory Management
In a FreeRTOS application, you want to allocate memory for a struct and initialize it immediately. Which code correctly combines pvPortMalloc and initialization?
AMyStruct *p; pvPortMalloc(sizeof(MyStruct)); *p = 0;
BMyStruct p = pvPortMalloc(sizeof(MyStruct));
CMyStruct *p = malloc(sizeof(MyStruct)); *p = {0};
DMyStruct *p = pvPortMalloc(sizeof(MyStruct)); if(p) *p = (MyStruct){0};
Step-by-Step Solution
Solution:
  1. Step 1: Allocate memory and check pointer

    MyStruct *p = pvPortMalloc(sizeof(MyStruct)); if(p) *p = (MyStruct){0}; allocates memory for struct and checks if pointer is not NULL.
  2. Step 2: Initialize struct using compound literal

    It initializes the struct to zero using compound literal syntax safely.
  3. Final Answer:

    MyStruct *p = pvPortMalloc(sizeof(MyStruct)); if(p) *p = (MyStruct){0}; -> Option D
  4. Quick Check:

    Allocate and initialize safely with pointer check [OK]
Quick Trick: Check pointer before initializing allocated memory [OK]
Common Mistakes:
  • Assigning pvPortMalloc result to struct variable
  • Using standard malloc instead of pvPortMalloc
  • Dereferencing uninitialized pointer

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FreeRTOS Quizzes