CS 70

References: Exercise 1

  • LHS Cow speaking

    Okay, now it’s your turn!

  • Hedgehog speaking

    Wait, really? I don’t think I’m ready!

  • LHS Cow speaking

    Sure you are! The reason we are teaching you how to draw these diagrams is that they will help you visualize what’s going on.

  • RHS Cow speaking

    Follow the rules, and you’ll be able to just look up what value is associated with each name.

Remember: Here are the new diagram rules:

  • When a function is called, allocate space for all local variables except references.
  • When a reference is initialized, write its name next to its referent.
  • When a reference is destroyed, cross out its name (the referent might not be destroyed at the same time!)

Now consider the following code snippet:

int x = 5;
int& z = x;
z = 10;
cout << x << " " << z;

Draw a memory diagram of this snippet and then give the printed output:

Did you really draw the diagram?

(When logged in, completion status appears here.)