12. After executing the statement M-MemoryManager(30), we have the following dia
ID: 3908372 • Letter: 1
Question
12. After executing the statement M-MemoryManager(30), we have the following diagram (header and trailer nodes omitted). 30 10 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 3940 (a) Show the contents of the pointer variables after executing the statement sequence below: p1-malloc(5); p2-malloc(10) p3-malloc(10); p4-malloc(5); p1-10 (b) Next showthe contents of the pointer variable p5 after execution of p2-15 p3-25 p4 35 free(p1); free(p2); p5malloc(12); p510Explanation / Answer
12
Initial memory address assigned by Memory Manager = 10
Number of locations in memory allocated = 30
(a)
p1 = malloc(5) // allocate 5 memory locations to p1
p1 = 10 // base address of p1 = 10
// assign 5 address locations to pointer variable p1(10,11,12,13,14)
p2 = malloc(10) // allocate 10 memory locations to p2
p2 = 15 // base address of p2 = 15
// assign 10 address locations to pointer variable p2(15,16,17,18,19,20,21,22,23,24)
p3 = malloc(10) // allocate 10 memory locations to p3
p3 = 25 // base address of p3 = 25
// assign 10 address locations to pointer variable p3(25,26,27,28,29,30,31,32,33,34)
p4 = malloc(5) // allocate 5 emory locations to p4
p4 = 35 // base address of p4 = 35
// assign 5 address locations to pointer variable p4(35,36,37,38,39)
(b)
free(p1); // deallocate memory locations from p1
free(p2); // deallocate memory locations from p1
p5 = malloc(12); // assign 12 memory locations to p5
p5 = 10;// base address of p5 = 10
// memory locations allocated to p5 are (10,11,12,13,14,15,16,17,18,19,20,21)
Do ask if any doubt. Please upvote.
p1=10 11 12 13 14 p2=15 16 17 18 19 20 21 22 23 24 p3=25 26 27 28 29 30 31 32 33 34 p4=35 36 37 38 39Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.