H10 Practice Soln

H10 Practice Soln - CS107 Handout #10 J Zelenski Dec 4,...

Info iconThis preview shows pages 1–3. 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: CS107 Handout #10 J Zelenski Dec 4, 2009 Solutions to final practice Final Exam: Friday, Dec 11th 8:30-11:30am Gates B01 and Gates B12 Problem 1: C coding a) Searching example stringtable for key "re" reports found match at offset 0. b) Searching example stringtable and key "blue" returns -1 because strstr will stop looking at first null. c) The short-circuiting behavior of && means a length mismatch will avoid the call to strcmp , but the two calls to strlen need to linearly traverse their inputs to find the terminating null and that is at least as much work as traversing both to compare (and usually more work because strcmp can stop at first mismatch). d) int AddString(struct strtab *st, char *key) { int offset = OffsetForString(st, key); if (offset != -1) return offset; offset = st->numUsed; // new string will be placed at end if (st->numUsed + strlen(key) + 1 > st->numAllocated) { st->numAllocated += strlen(key) + 1; st->data = realloc(st->data, st->numAllocated); assert(st->data != NULL); } strcpy(&st->data[st->numUsed], key); // strcpy appends null st->numUsed += strlen(key) + 1; return offset; } Problem 2: Data layout and access a) return f->seeds[f->ripe]; b) leal 0xc(%ebp),%ecx # addr for RHS (first field of struct) movl 0x8(%ebp),%edx # load value of param x movl 0x30(%ebp),%eax # load value m.cost (base addr) movl %ecx,(%eax,%edx,4) # add scaled x to base & store elem 2 Problem 3: Reading/tracing IA32 a) There are no local variables. b) There are two parameters, at ebp + 8 is an int* and at ebp + 12 is an int . c) Given an integer array and its effective size, the function recursively traverses the array and returns a pointer to the first element with value 4 or NULL if no 4 is found. Problem 4: Stack bool HasRecursion(int arg) { void **bp = (void **)&arg - 2; // get bp, 2 ptrs beneath 1st param while (1) { if (*bp == NULL) // if saved bp NULL, end of stack return false; if (*(bp + 1) == *((void **)*bp + 1)) // ra is 1 pointer past bp return true; bp = (void **)*bp; // follow bp to saved bp (next frame)...
View Full Document

Page1 / 4

H10 Practice Soln - CS107 Handout #10 J Zelenski Dec 4,...

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

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