ProgrammingHard
int *p = malloc(10 * sizeof *p); free(p); free(p); What is the result?
- A. The second free is a no-op
- B. Undefined behaviour, because p is freed twice
- C. It frees 20 ints, since the size doubles
- D. A runtime error is guaranteed
Correct answer: B. Undefined behaviour, because p is freed twice
Freeing the same pointer twice is undefined. C does not promise a no-op or an error.
More C Programming questions
- 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?
- Which function call allocates storage for an array of 100 doubles, all set to 0.0, in one step?
- A function gets a pointer to a linked list and must keep the list unchanged but needs to walk it twice. Which kind of pointer parameter is…
- A program keeps 10^5 small objects in a free list of pointers. Which operation is O(1) per object?