Because C++ uses the = symbol for assignment, it is tempting to interpret the statement a = b as a statement of equality. It is not! The best way to think of what the symbol means is “take the value that is on the right side and store it into the location on the left side”. For that reason, programmers often pronounce = as “gets set to” or just “gets”. Thus a = 5; is not “a equals 5” but “a gets set to 5”
Equality is commutative, and assignment is not. For example, in mathematics if \(a = 7\text{,}\) then \(7 = a\text{.}\) In C++ a = 7; is a legal assignment statement, but 7 = a; is not. The left side of an assignment statement has to be a variable name (storage location).
Also, in mathematics, a statement of equality is true for all time. If \(a = b\) now, \(a\) is always equal to \(b\text{.}\) In C++, an assignment statement can make two variables equal, but they don’t have to stay that way. This Codelens lets you run the code in this sample one line at a time. As you do so, watch the area labeled Stack. You will see the variables in that area and you will see their values change as the code runs.
The second line sets b to have a copy of the value a has. That does mean that temporarily they are equal to each other. But b = a does not permanently equate the two. It only copies the value that a has at that exact moment into the memory labeled b .
Taken together, the variables in a program and their current values make up the program’s state. The Codelens tool allows you to watch the state of the program change as you step through the code. Other tools also let you see the state of the program as it runs. A debugger is a program designed to show the state of a program being developed as it runs. When developing code outside of this book, you will use a debugger to help you find and fix errors in your code.
But you can also represent the state of a program with a human created memory diagram. For example, Figure 3.5.2 shows the state of the program after these assignment statements run:
Diagrams like this one that show the state of the program are called memory diagrams. Each variable is represented with a box showing the name of the variable on the outside and its current value inside.
As the program runs, the state of memory changes, so memory diagrams show only a particular point in time. For example, if we added the line int c = 0; to the previous example, the memory diagram would look like Figure 3.5.3.
As a programmer seeking to understand what code is doing, you should create a memory diagram that represents the state of the program and update that diagram after each line of code. When you are starting out, or debugging a very complex piece of code, you may find it helpful to literally draw the diagram on paper or a whiteboard. As you get more experienced, you will will get better and better at creating a mental memory diagram of the code you are reading.
Just reading code is not a very useful exercise unless it is accompanied by work to understand what you are reading. Creating a mental or physical memory diagram, and then ideally checking the accuracy of your diagram with a tool like Codelens or a debugger, is a key way to develop real understanding of code.