midterm - CPSC 213 Winter 2009 Term 2 — Midterm Exam...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CPSC 213, Winter 2009, Term 2 — Midterm Exam Solution Date: March 12, 2010; Instructor: Mike Feeley 1 (2 marks) Memory Addresses. Give an example of a memory address that is aligned to an four-byte boundary, but not to an eight-byte boundary. 4 2 (4 marks) Pointer Arithmetic. Consider the following three lines of C code. For the assignments to i and j , say (a) whether the code generates a runtime error and why or (b) what value the variables have after the code executes. If the first generates an error and the second does not, give the value of the second ignoring the first. Show your work. int a[10] = { 0,1,2,3,4,5,6,7,8,9 } ; int i = * (a + ((&a[7])-a) + * (a+2)); int j = * (a + * ((&a[7])-a) + * (a+2)); 2a i : No error. Value of i is 9. 2b j : This statement will attempt to de-reference the address ((&a[7]-a) == 7 , which would probably generate a runtime error. On a non-Intel architecture it will be an error, because 7 is an unaligned address. The Intel ISA allows unaligned addresses, but on most operating systems, page 0 is protected and so attempting to read address 7 generates an invalid address error. To answer this question correctly, you can say either ”it will generate an error” or ”it will read memory at address 7, which it outside of the array” or something like that. 3 (4 marks) Dynamic Allocation. A dangling pointer exits when a program retains a reference to dynamically allocated memory after it has been freed/reclaimed. A memory leak exists when a program fails to free/reclaim dynamically allocated memory that it is no longer needed by the program....
View Full Document

This note was uploaded on 10/11/2010 for the course CPSC cs213 taught by Professor Mikefeely during the Spring '10 term at UBC.

Page1 / 4

midterm - CPSC 213 Winter 2009 Term 2 — Midterm Exam...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online