Bird
0
0

What will be the output if this FreeRTOS task function runs and prints the parameter cast as int?

medium📝 Predict Output Q4 of 15
FreeRTOS - Task Creation and Management
What will be the output if this FreeRTOS task function runs and prints the parameter cast as int? void MyTask(void *pvParameters) { int value = (int)pvParameters; printf("Value: %d\n", value); vTaskDelete(NULL); } Called with (void *)42;
ACompilation error
BValue: 0
CRuntime crash
DValue: 42
Step-by-Step Solution
Solution:
  1. Step 1: Analyze parameter casting

    Directly casting a pointer to int like (int)pvParameters is unsafe on many systems and can cause errors.
  2. Step 2: Understand pointer to int cast risks

    On 64-bit systems, pointers are 64-bit but int is 32-bit, causing truncation or invalid values, often leading to runtime crashes. However, on 32-bit systems or when passing small integer values cast as pointers, this can work and print the expected value.
  3. Final Answer:

    Value: 42 -> Option D
  4. Quick Check:

    Direct cast of small integer to void* and back prints value on 32-bit systems [OK]
Quick Trick: Casting small integer to void* and back may work on 32-bit systems [OK]
Common Mistakes:
  • Assuming direct cast is always unsafe
  • Ignoring pointer size differences
  • Expecting runtime crash always

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FreeRTOS Quizzes