Today's lecture starts a two-part sequence where we'll consider a conceptual model of the underlying machine, with particular attention to objects in memory, their addresses, and the values they hold. We'll also introduce references and pointers, two fundamental tools for working with objects indirectly.
Why do we need to work with objects indirectly in programming? It turns out we often work/think indirectly in real life, but may not realize it. For example, an address book refers indirectly to the places that people live, but it doesn't literally contain those places! (That wouldn't even make sense.)
Likewise, in a program we might want several different parts of our code to refer to the same data structure, but we don't want them all to literally have a local copy of that data. It would be better to use a pointer to store the address of the data and just go look it up when we need to.
We're sharing an announcement on behalf of the RenewCS tutoring and mentoring program.
1: Default Initialization
|
2: Arithmetic Operations
2.1
2.1 Exercise: Modular Arithmetic
Say you have a Matrix of width W and height H, and that each cell in the matrix is labeled with an index. For example, a matrix with width 5 and height 3 would look like this: You can use
Consider also tracking the current player in a turn-taking game. For example, if you're playing the card game Uno, you might have a circle of 6 players numbered 0-5: Complete the line of code below to update the
You're welcome to check your solution with this walkthrough video: |
3: Relational Operations and Floating-Point Precision
|
4: Value Semantics, Addresses, and References
4.1
4.1
Consider the following code:
Which of the following are true? Sample solution… The value of The value of The value of The expression &x == &z will always yield true. If the line |
5: Intro to Pointers
5.1
5.2
5.1 Exercise: Pointer Fundamentals
You're welcome to check your solution with this walkthrough video:
5.2 Exercise: Using Pointers
Tip: Drawing memory diagrams is a great way to reason about code. Let's get some practice in now! You'll thank yourself later on some of the more complex projects, and it's also a great way to prep for exams.
Mentally trace this code and draw a memory diagram as you go. Once you're finished, use your diagram to answer the question below. You could click "Simulate" to walk step-by-step through the program and verify your work matches the visual simulation. What values are printed for each of the expressions sent to
Sample solution…x ptr &x *&x
|
6: Pointer Debrief
|
7: Expression Value Categories
7.1
7.1
Which of the following are true about expression value categories in C++? Sample solution…An lvalue is an expression that yields an object at some memory location. An lvalue may appear on the left hand side of an assignment. An lvalue may appear on the right hand side of an assignment. An rvalue may appear on the left hand side of an assignment. An rvalue may appear on the right hand side of an assignment. |