CS360 Midterm -- March 14, 2002. Answer/Grading for Question 4


Answer

Part A. 49 has to be rounded up to the nearest multiple of 8: 56. Then eight bytes of bookkeeping are added. Therefore each malloc() statement actually allocates 64 bytes. Since there are 1000 malloc() calls, this is 64,000 bytes.

Part B. The malloc() buffers are 8K. Therefore, there are ceil(64000/8192) = 8 sbrk() system calls.

Part C. You are trying to write to a byte two bytes past the end of the heap. Therefore, you would think that this should generate a segmentataion violation. However, it is very unlikely that it actually will generate a segmentation violation. Why? Remember the lecture on memory. Memory is actually doled out from the operating system in pages, which are usually 4K or 8K in size. Therefore, even though sbrk(0) may return, for example, 0x21e18, we will not generate a segmentation violation until we use address 0x22000. However, if sbrk(0) returns an address that is right at a page boundary, then this code will generate a segmentation violation.

So the answer is ``maybe, but it is extremely unlikely.'' If you don't understand this point, see the sbrk(0) part of the Memory Lecture.

Part D. Again, this is explained in the memory lecture. You are setting s to be a pointer in the code segment, which means that you can read it, but not write it. Therefore, you will generate a segmentataion violation, but at line 5.

Grading - 11 Points