Difficulty: Medium
Correct Answer: Garbage value
Explanation:
Introduction / Context:
This question examines behavior after freeing dynamically allocated memory. It also highlights that printing pointers with %u is non-portable (the correct specifier is %p). The central concept is undefined behavior and dangling pointers.
Given Data / Assumptions:
Concept / Approach:
After free, p becomes a dangling pointer: its stored bits may remain the same, but using it is undefined behavior. Even reading or printing it in a non-portable manner can produce unpredictable results. Therefore, you cannot rely on seeing 1314 or any specific value. The robust practice is to set p = NULL after free and print with %p if needed.
Step-by-Step Solution:
Verification / Alternative check:
Setting p = NULL after free and then printing with printf("%p", (void)p) yields a consistent "(nil)" style representation on many systems, demonstrating defined behavior.
Why Other Options Are Wrong:
Common Pitfalls:
Believing that free changes the pointer value or that printing a freed pointer is safe. Always invalidate the pointer explicitly.
Final Answer:
Garbage value
Discussion & Comments