ProgrammingHard
A doubly linked list node has prev and next. What must happen before freeing the node p in the middle of the list?
- A. Only p->next must be set to NULL
- B. The node must be copied to the stack first
- C. Both neighbours must be linked to each other, bypassing p, before free(p) is called
- D. Nothing, because free unlinks the node automatically
Correct answer: C. Both neighbours must be linked to each other, bypassing p, before free(p) is called
free does not touch the list, so the neighbours must skip over p first. Otherwise they keep pointers to freed memory.
More C Programming questions
- A linked stack stores nodes with a pointer to the top. Push and pop run in O(1). What is the cost of freeing the whole stack when it has n…
- A list of 10^6 nodes is built by prepending, and then each node is looked up by value in a loop. What is the total cost of 10^6 lookups?
- int p = malloc(10 sizeof p); free(p); free(p); What is the result?
- In C, which of these are true about malloc's return value (assume the request succeeds)?
- A program allocates an int array of size n with malloc(n sizeof(int)). For n = 2^31, on a platform with 32-bit sizet, what can go wrong?
- A malloc'd block of 16 bytes is written past its end by 4 bytes. What is the correct description?