ProgrammingModerate
Which statement about calling free on a null pointer is correct in C?
- A. It does nothing, so free(NULL) is safe
- B. It is undefined behaviour and must be avoided
- C. It frees the memory at address zero
- D. It is a compile error
Correct answer: A. It does nothing, so free(NULL) is safe
The standard defines free(NULL) as a no-op. This makes cleanup code simpler, since no test is needed.
More C Programming questions
- A pointer p is freed with free(p) and then set to NULL. Why is this safer than leaving it dangling?
- Which code creates a flexible structure holding n ints after a header, with a single allocation and correct size?
- A tail-insertion function takes a pointer to the head pointer, so it can work on an empty list. Which loop finds the link to change?
- A queue with head and tail pointers is emptied by dequeuing its only node. What must the dequeue do to keep the queue correct?
- A recursive function removes all nodes equal to x from a singly linked list and returns the new head. Which return statement is the right…
- A program creates an array of 10^6 pointers and then one malloc block per pointer, with 10^6 frees at the end. What is the total number of…