Skip to main content

Section 18.1 Memory Addresses of Variables

One of the benefits of programming in a high-level language like C++ is that we usually do not have to worry about the details of how the computer stores our data. We can just declare int x = 10; and the compiler will do the work of deciding where to place that data into memory and keeping track of the location so when we next say x the code accesses that same location. However, there are times when it is useful to work directly with the memory address of a variable - to be able to say β€œwork with the data stored at location X”.
In C++, to access the memory address of a variable, we can use the address-of operator &. To do so, we place it to the left of a variable name. For example, if we have a variable x, we can get its memory address using &x. The example below will print the memory address of x and y:
Listing 18.1.1.
Running the program should produce something like:
Address of x: 0x7ffc622069c0
Address of y: 0x7ffc622069c4
The 0x indicates that these are hexadecimal values. The rest of the digits are the actual address. Note that the two values, although quite large, are only 4 apart. Since we declared x and y one after another, and in this code, an int takes 4 bytes, the address of y is 4 more than the address of x. If we diagrammed the memory at a very low level, it might look like this:
x occupies 4 bytes starting at 0x7ffc622069c0. It stores the binary value for 10. y occupies 4 bytes starting at 0x7ffc622069c4. It stores the binary for 14.
Figure 18.1.2. Low Level Memory Diagram for x and y
In the diagram, we can see that what we call xare the 4 bytes starting at 0x7ffc622069c0(0x7ffc622069c0-0x7ffc622069c3). It stores the binary value for 10. What we call y are the 4 bytes starting at 0x7ffc622069c4. It stores the binary for 14. Even when we are dealing with memory addresses directly, that diagram is more detailed than we actually need. Here is a higher level view of the same memory:
x is at 0x7ffc622069c0 and has the value 10. y is at 0x7ffc622069c4 and stores 14.
Figure 18.1.3. Mid Level Memory Diagram for x and y
This version focuses on what we actually need to know. It shows us that x is at 0x7ffc622069c0 and has the value 10. It also shows us that y is at 0x7ffc622069c4 and stores 14. We are describing it as a mid-level diagram because it includes some of the low level details - the memory addresses - but not all of them. A true high-level memory diagram, like we have used in the past, would not show us the addresses at all. It would just show us that x is 10 and y is 14.

Note 18.1.1.

Unfortunately the & symbol can mean multiple things in C++. It can mean:
  • That a variable is a reference. It means this when the & is placed to the right of a type. For example, in the function header void foo(int &x), the & means that x is a reference to an integer. The & is modifying the type of x.
  • The address-of operator, which gives the memory address of a variable. It means this when it is to the left of a variable name and there is no type specification. In other words, when we are using a variable, not declaring one, like cout << &x;
  • Logical or bitwise AND. It means this when it is placed between two variables. For example, in the expression x && y, the & means to do a bitwise AND operation on the values of x and y. x amp; y means to combine the two values bit by bit to create a new value (not covered in this book).

Checkpoint 18.1.1.

In the statement cout << &x what is the & doing?
  • It gives the memory address of x.
  • It indicates that the x variable is a reference.
  • It is doing bitwise AND.

Checkpoint 18.1.2.

In the statement double &x; what is the & doing?
  • It gives the memory address of x.
  • It indicates that the x variable is a reference.
  • It is doing bitwise AND.

Checkpoint 18.1.3.

In the statement void mutate(int &x;); what is the & doing?
  • It gives the memory address of x.
  • It indicates that the x variable is a reference.
  • It is doing bitwise AND.

Checkpoint 18.1.4.

In the statement mutate(&x); what is the & doing?
  • It gives the memory address of x.
  • It indicates that the x variable is a reference.
  • It is doing bitwise AND.
You have attempted of activities on this page.