ProgrammingVery hard
A malloc'd array of 10 structs is later passed to free(&arr[3]). What is the correct description?
- A. It frees only the fourth struct
- B. It is harmless because free ignores the argument
- C. Undefined behaviour, because free must receive the exact pointer that malloc returned
- D. It frees the entire array and sets arr to NULL
Correct answer: C. Undefined behaviour, because free must receive the exact pointer that malloc returned
free accepts only a pointer that malloc, calloc or realloc returned, or NULL. A pointer into the middle is not valid.
More C Programming questions
- Which declaration allocates a 3 x 4 int matrix as one contiguous block with a single free needed?
- 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?
- 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}?