Question 3 (18 points, 30 minutes)
Part 1 (12 points)
Describe (in English, not using code) how Unix's malloc() and
free() work.
Part 2 (6 points)
When the following program reaches the HERE
line, state how many bytes have really been allocated in the heap.
Partition these bytes into:
- Bytes that user user thinks he has allocated.
- Bookkeeping bytes.
- Bytes of padding.
- Bytes in the free list.
Assume that malloc() calls sbrk() in 8K chunks.
typedef int int253[253];
main()
{
int253 *all[1024];
int i;
for (i = 0; i < 1024; i++) {
all[i] = (int253 *) malloc(sizeof(int253));
}
free(all[500]);
free(all[501]);
/* HERE */
}
You may express your answers as equations. One product you may want
to know is 253 * 4 = 1012.