22-Section-Handout

22-Section-Handout - CS106X Autumn 2010 Handout 22 October...

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

View Full Document Right Arrow Icon
CS106X Handout 22 Autumn 2010 October 18 th , 2010 Section Handout Problem 1: Farmers Market Memory Trace Analyze the following program, and draw the state of memory at the two points indicated. Be sure to differentiate between stack and heap memory, mark un-initialized values, and identify where memory has been orphaned. struct fruit { int apple; bool banana; int *cherry[4]; fruit **date; }; int main() { fruit salad; salad.apple = 7; salad.cherry[0] = &salad.apple; salad.cherry[1] = *(salad.cherry); salad.date = new fruit *[6]; salad.date[0] = NULL; salad.date[1] = new fruit; salad.date[1]->cherry[2] = new int[3]; bool *currant = &salad.banana; currant[0] = false; FarmersMarket(salad, *(salad.date[1]), &salad.date[1]); return 0; } void FarmersMarket(fruit grape, fruit& lemon, fruit **lime) { if (grape.banana) { lime[1] = new fruit; lime[1]->date = &lime[2]; lime[0] = lime[1]; lemon.cherry[1][0] = 17; return; } grape.cherry[2] = &lemon.apple; grape.apple *= 2; lemon.apple = 20; lemon.banana = true; FarmersMarket(lemon, grape, &lime[1]); lemon.date = NULL; } Draw the state of memory here, before the function call. And draw the state of memory here, just before return.
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Problem 2: The Sparse String Vector A SparseStringVector is a Vector<string> -like data structure that provides super fast access to its elements, and near constant-time insertion and deletion. It layers array semantics over an ordered collection of C++ strings, with the understanding that most of the strings are empty . The SparseStringVector is different from our C++ Vector and other array-like data structures, because it’s stingy in its use of memory. Each empty string
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

22-Section-Handout - CS106X Autumn 2010 Handout 22 October...

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