Two problems associated with pointer types are dangling pointers and lost heap d
ID: 3693901 • Letter: T
Question
Two problems associated with pointer types are dangling pointers and lost heap dynamic variables.
a) Briefly describe each of these problems and explain why it is considered a problem.
b) Consider the following code in C++:
int * a;
int * b = new int [3];
a = b;
b[0] = 5; b[1] =6; b[2] = 7;
delete b;
int * c = new int[3];
// Asssume that deleted space for b is allocated to c.
c[0] = 1; c[1] = 2; c[2] = 3;
a[1] = 10 ; a[2] = 11;
What are the values of c[0], c[1], and c[2] after this code executes? Explain.
c) Assume the code in part(b) is part of a subprogram that terminnates immediately after the last statement (so there is no "delete c" statement). If this subprogram is called 20000 times, what is the effect on available memory in the heap, assuming each int is four bytes long? Explain you answer.
Explanation / Answer
a.
Pointer Problems
b. The values are 1,2,3 respectively. The assignment of 10,11 into array 'a' has no bearing on array 'c'. Its a pretty straightforward answer.
c.