Consider a programming language with pointer types and an explicit dereferencing
ID: 3721929 • Letter: C
Question
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 emergence 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 (without clearing it), but the pointer still points to the old place.
(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, *q, *r;
p = new int;
*p = 5;
q = p;
free (p);
p = null;
write (*q);
r = new int;
*r = 17;
write (*q);
Explanation / Answer
A programming language with pointer types and an explicit dereferencing operator. The programmer reallocates’ memory and there is no safety mechanism implemented to perceive the appearance of dangling references.
Suppose that assigning to a pointer the value null does not free the memory it pointed to, it simply sets the pointer value to null, ie the pointer now does point anywhere. Free frees the memory (without clearing it), but the pointer still points to the old place.
int *p,*q,*r;//pointer variable declaration
p= new int; //allocating memory and linking p
*p=5; //setting value...in memory
q=p; //linking to the memory location of 5
free (p) ;//removing memory location p is linked to
p=null;
write(*q); //nothing will be printed....because memory cleared
r = new int; //allocating memory and linking r
*r = 17; //setting value..in memory
write (*q);//nothing will be printed....because memory cleared
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.