ProgrammingModerate
A list of N nodes is freed with free() on each node but the next pointer is read after freeing it. Which order is correct?
- A. Save next in a temporary before calling free on the current node
- B. Read next after free, since the block is still valid
- C. Free the nodes in random order
- D. Set every next to NULL before freeing, which frees nothing
Correct answer: A. Save next in a temporary before calling free on the current node
free invalidates the node's memory, so the next pointer must be copied out before the free call.
More C Programming questions
- C(0) = 1 and C(n) = sum over i from 0 to n-1 of C(i) C(n-1-i). What is C(4)?
- A 3 x 3 grid holds 1 to 9 row by row, so row 0 is 1 2 3, row 1 is 4 5 6 and row 2 is 7 8 9. What is the sum of the 2 x 2 block with rows 1…
- In the sorted array {1, 2, 4, 6, 8, 9}, how many unordered pairs of distinct elements sum to 17?
- What is the largest sum of any three consecutive elements of {2, 1, 5, 1, 3, 2}?
- The sorted array is {1, 3, 6, 6, 8}. What is the index of the first occurrence of 6?
- How many integers in the range 0 to 15 have an even number of 1 bits in binary?