Question: 4. Consider a programming language with pointer types and an explicit dereferencing operator. The programmer deallocates memory and there is no safety mechanism implemented to


4. Consider a programming language with pointer types and an explicit dereferencing operator. The programmer deallocates memory and there is no safety mechanism implemented to detect the emer- gence of dangling references. Suppose that assigning to a pointer the value null does not free the memory it pointed to, it merely sets the pointer value to null, ie the pointer now does point anywhere. free frees the memory pointed to by a given pointer without clearing it; it also leaves the pointer pointing there (i) What does the following program print? Explain int *p, *q p = new int; *q = 5; write (*p); write (*q); (ii) What will the following program print? Explain int *p, p = new int; free (p); write (*q) write (*p); (iii) What will the following program likely print? (Assume that free heap memory is tracked with a free list, so that the memory freed last will be taken into use anew first.) Explain int *p, *, s; p = new int; q=p; *q = 5; free (p) p = null; free (q); r = new int; *r = 17; write q); s-new int; write *s)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
