Section 9. Final Review

Section materials curated and created by Yasmine Alonso.

Happy last week of section! This final section serves as a final exam review session. We hope today’s section helps you feel a little more prepared for the final :) You’ve got this! No starter code today, we’re just providing one memory diagram problem for extra practice, and then allowing you and your SL decide what to review for whatever time is left. Thanks for a great quarter!

1. Memory Diagram

a) Draw out a stack/heap memory diagram for the following snippet of code!

struct Beverage {
	string name;
	int mgCaffeine;
};

struct SectionLeader {
	string name;
	Beverage* favBeverage;
	int numQuartersSLing;
};

SectionLeader* SLs = new SectionLeader[3];
SLs[0].name = "Anushka";
SLs[1].name = "Tommy";
SLs[2].name = "Lucas";

SLs[0].numQuartersSLing = 2;
SLs[1].numQuartersSLing = 7;
SLs[2].numQuartersSLing = 3;

Beverage* matcha = new Beverage;
matcha->name = "matcha latte";
matcha->mgCaffeine = 65;

Beverage* coffee = new Beverage;
coffee->name = "Tokyo latte"

SLs[0].favBeverage = matcha;
SLs[1].favBeverage = coffee;
SLs[2].favBeverage = matcha;

coffee->mgCaffeine = 2 * (SLs[2].numQuartersSLing) + 3 * (matcha->mgCaffeine);

int* caffeine = &((SLs[0].favBeverage)->mgCaffeine);

// What does this print?
cout << "Today I have consumed " << *caffeine << " mg of caffeine." << endl;

// DRAW THE MEMORY AS IT LOOKS HERE!

b) If we changed our diagram from having the Tokyo latte Beverage box be moved from the heap side to the stack side, what changes in our code would we have to make to reflect that?

Here is what the diagram should look like after part a). The print statement should print out: “Today I have consumed 65 mg of caffeine.”

2. Students’ Choice!

With whatever time remains, please feel free to choose another practice problem (from section, lecture, IGs, anything!) on a subject you’d like to review.

3. Say Goodbye!

Thanks for a great quarter! We hope you all learned a lot and had fun while doing so. You’ve come so far since the start of the course! This is likely the last time you will see your SL (unless you haven’t had A6 IGs yet), so make sure you say goodbye :'(