In the previous section, we discussed variables and I mentioned that the computer stores each variable at some address on the computer. In this section, we will see where variables are stored and how to find this information.
In most modern computers, each byte in main memory has a different address. That is, the computer is said to be byte addressable. Figure 1 shows all of main memory on the left-hand side and zooms in on two locations, showing how each byte has its own address. Each address can store a value between 0 and 255 (1 byte or 8 bits). Usually, the address of a byte is shown as a hexadecimal number (base 16), though this is only through convention. If the hexadecimal numbers in Figure 1 seem confusing at this point, just replace them within integers going from 0 to 4294967296.
Figure 1. Memory map.
If you take a look at Program 1, you will note that main has exactly one variable named value.
Program 1. An integer (int) variable in main.
#include <iostream> using namespace std; int main() { int value = 293; cout << "The variable 'value' is storing " << value << endl; return 0; } |
To see where the variable value is being stored in main memory, we must ask for its address. We get the address by prefixing value with an &. You can think of &value to represent the address of value. (The symbol & is called the ampersand.) Program 2 prints the value of the variable value but also prints out the address.
Program 2. Printing the address where 'value' is stored.
#include <iostream> using namespace std; int main() { int value = 293; cout << "The variable 'value' is storing " << value << endl; cout << "The address at which 'value' is being stored is " << &value << endl; return 0; } |
When I run this, I get the following output:
The variable 'value' is storing 293 The address at which 'value' is being stored is 0xffbefc6c
First, the 0x means that the number which follows is in base 16. You will learn about this in your other courses and you don't need to know this for ECE 250 or for understanding C++.
When you run this program, the first result will be the same no-matter-what, however, the second value may differ: your computer may choose to store the variable somewhere else in main memory. On my computer, this says that the first byte of the integer is stored at the 4290706540th byte (the base-16 number FFBEFC6C16 converted to decimal) in memory.
As mentioned previously, an integer actually takes up four bytes, and therefore if we zoom in on the memory map, the memory would look like that shown in Figure 2. The red address is the address of the first byte while the four red boxes represent the four bytes in which 293 is stored. You do not have to worry about how 293 is stored: you will learn that in another class.
Figure 2. Memory map showing where 293 is stored (on my computer).
Program 3 shows that if the value of a variable is changed, the address remains the same. If you were to run the program, the first time, you would see that the address remains unchanged.
Program 3. Example showing that the address does not depend on the value stored.
#include <iostream> using namespace std; int main() { int value = 293; cout << "The variable 'value' is storing " << value << endl; cout << "The address at which 'value' is being stored is " << &value << endl; value = -932; cout << "The variable 'value' is now storing " << value << endl; cout << "The address at which 'value' is being stored is still " << &value << endl; return 0; } |
What's important here is that since we know the address of a variable, then we should be able to tell the computer to change what is at that memory location.
1. Write a program which has two variables: int i = 523; and int j = -253. Print out their addresses. What do you notice about the two addresses (recalling that integers are stored using four bytes).
2. Take Program 1 and change the name of the variable. Does this affect the location at which it is stored?
3. How could you use the result from Question 2 to test for plagiarism, that is, where one student copies another student's code but changes all the variable names?