CS360 Midterm -- March 14, 2002. Question 4

Behold the procedure on the right.

Answer the following questions:

a(char *array[1000])
{
  int i;

  for (i = 0; i < 1000; i++) {
    array[i] = (char *) malloc(49);
  }
}

Part A. How many bytes are actually allocated on the heap by this procedure?
Part B. How many sbrk() system calls are made when this code is executed if malloc() buffers in 8K chunks?
Part C. Is the following piece of code going to cause a segmentation violation? Explain your answer in detail, including where in the code the segmentation violation occurs if indeed it does occur.
{
  char *s;         /* Line 1 */

  s = sbrk(0);     /* Line 2 */
  *s[1] = 0;       /* Line 3 */
}

Part D. Is the following piece of code going to cause a segmentation violation? Explain your answer in detail, including where in the code the segmentation violation occurs if indeed it does occur.
int a()
{
  char *s;           /* Line 1 */
  char c;            /* Line 2 */

  s = (char *) a;    /* Line 3 */
  c = *s;            /* Line 4 */
  *s = c;            /* Line 5 */
}