11-destructive-and-non-destructive-operations-1

# 11-destructive-and-non-destructive-operations-1 - Computer...

This preview shows pages 1–6. Sign up to view the full content.

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

View Full Document

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

View Full Document

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Computer Science 136: David R. Cheriton School of Computer Science Elementary Algorithm Design and Data Abstraction Winter 2012 Lecture 11 - Destructive and Non-destructive List Processing The list operations we have seen in Racket, and in C so far, have all been non-destructive (returning “new” lists, and not modifying the input). C offers the ability to modify lists after they are created (as does Racket, though we won’t go into this). This is both powerful, and tricky. We examine techniques and pitfalls. CS 136 – Winter 2012 11 – Destructive and Non-destructive List Processing 1/21 Advice on VirtualBox From the VirtualBox Readme: 3. A virtual machine can become corrupted. Therefore, you should periodically make copies of your work someplace else. An easy way to do this is: a. click the terminal icon (same as reading news) b. type "uwbackup" (without the quotes) c. this copies your files to your account on UW’s computers- cs136Assignments- Desktop- Documents You should do this fairly often. Perhaps every time you stop working on your CS136 assignments. CS 136 – Winter 2012 11 – Destructive and Non-destructive List Processing 2/21 Destructive vs. non-destructive Non- destructive has a precise meaning: An operation is non-destructive if it promises not to mutate its arguments. Consider this specification of iappend : ilist iappend(ilist il1, ilist il2); // builds a new list with the elements // of il1 followed by the elements of il2 Builds a new list means: New memory is possibly allocated for the appended list. Memory allocated to il1 and il2 is left intact. “Destructive” is less precise: An operation is “destructive” if it makes no promises about not mutating its arguments. It does promise to re-use or free all memory reachable from the argument to be destroyed CS 136 – Winter 2012 11 – Destructive and Non-destructive List Processing 3/21 Copying an ilist in C? Assume the ilist specification and implementation from Assignment 5. Suppose my program makes the following calls: ilist x = icons(4, icons(1, iempty())); ilist y = icons(5, x); ilist z = icons(3, icons(3, iempty())); ilist w = z; How many “independent” lists are on the heap at this point? Where do the variables point? Is ilist w a copy of ilist z ? What calls to idelete are necessary to free all memory? idelete(y); idelete(z); or idelete(w); but not both. CS 136 – Winter 2012 11 – Destructive and Non-destructive List Processing 4/21 Copying an ilist in Racket?...
View Full Document

{[ snackBarMessage ]}

### Page1 / 21

11-destructive-and-non-destructive-operations-1 - Computer...

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

View Full Document
Ask a homework question - tutors are online