ProgrammingModerate
A malloc'd block of 16 bytes is written past its end by 4 bytes. What is the correct description?
- A. Undefined behaviour, which can corrupt the heap and may not crash immediately
- B. It is harmless, because malloc adds padding
- C. It is guaranteed to crash at once
- D. It writes into the next allocation in a defined way
Correct answer: A. Undefined behaviour, which can corrupt the heap and may not crash immediately
Writing past the end of an allocation is undefined. The damage may stay hidden until a later allocation or free fails.
More C Programming questions
- 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?
- int arr = malloc(5 sizeof arr); for (int i = 0; i < 5; i++) arr[i] = i i; int q = arr + 2; printf("%d", q[1]); What is printed?
- struct N { int v; struct N next; } a = NULL; for (int i = 1; i <= 3; i++) { struct N n = malloc(sizeof n); n->v = i; n->next = a; a = n; }…
- Two pointers p and q point into the same block allocated with malloc(10 sizeof(int)). Which subtraction is defined?