ProgrammingModerate
A function frees an entire list and then sets head = NULL. Why is setting head to NULL useful?
- A. It causes free to return memory to the operating system immediately
- B. It makes the next allocation return the same block
- C. It is required by the C standard for every free
- D. It stops later code from reading or freeing the old, now dangling, pointer
Correct answer: D. It stops later code from reading or freeing the old, now dangling, pointer
A NULL pointer is safe to test, and a second free of a NULL pointer is a no-op. A dangling pointer is a hazard.
More C Programming questions
- Which kind of list makes it O(1) to delete a node given only a pointer to that node?
- In a singly linked list, which operation takes O(1) time when a pointer to the head is given?
- Two pointers walk a linked list: fast goes two nodes per step, slow goes one. The list has an even number of nodes N = 8 and no cycle…
- A cyclic list has a head node whose next points back to itself. What does a naive while (p) loop starting from head do?
- In C, what is the value of a freshly malloc'd int that the program never initialises?
- A linked list is reversed with the recursive function rev(n): if (!n || !n->next) return n; h = rev(n->next); n->next->next = n; n->next =…