Machine Model, Part 1

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.

Updated Fall 2024

1: Default Initialization




2: Arithmetic Operations
2.1 Not Started


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 / and % to compute the row and column based on the index. Fill in the appropriate operator and variable in each box below.

int width = 5;
int height = 3;
int index = 13;
int row = ; // compute row, e.g. index 13 has row 2
int col = ; // compute col, e.g. index 13 has col 3

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 current index to the next player. For example, if current was 3, it should be updated to 4. But, if current was 5, it should be updated and wrap back around to 0. You can use % to help implement this.

int num_players = 6;
int current = ??;

// update current player
current = (current + 1) ;


You're welcome to check your solution with this walkthrough video:




3: Relational Operations and Floating-Point Precision




4: Value Semantics, Addresses, and References




5: Intro to Pointers
5.1 Not Started 5.2 Not Started


5.1 Exercise: Pointer Fundamentals
Consider the program below and answer a few questions.
int main() {
  int x = 4;
  int y = 7;
  double z = 1.5;

  int *ptr1 = &x;
  int *ptr2 = &y;
}
If you added the line cout << ptr2 << endl; to the end of main(), what would be printed?
Suppose you added the line ptr1 = &ptr2; to the end of main(). Would the compiler allow this? If so, what would the effect of that line be?
Suppose you added the line ptr1 = ptr2; to the end of main(). Would the compiler allow this? If so, what would the effect of that line be?
Would it be possible to change the value of z using either of the two pointers declared in main()? If so, how? If not, why not?


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 cout at the end of the program?

x      ptr        &x         *&x    

y     *ptr     &ptr     &*ptr    

Sample solution… x      ptr        &x         *&x    

y     *ptr     &ptr     &*ptr    



6: Pointer Debrief




7: Expression Value Categories


You've reached the end of this lecture! Your work on any exercises will be saved if you re-open this page in the same web browser.

Participation Credit
Make sure to sign in to the page, complete each of the exercises, and double check the participation indicator at the top left of this page to ensure you've earned credit.