Computer Architecture: Memory Addresses

Memory Addresses

In this Computer Architecture video, we demonstrate how to display the memory addresses of data variables. Specifically, we use the C++ programs below, which can be copied an pasted into a simple C++ project like the one we created in C++ Console lesson 1.

Memory

The main memory in most computers consists of byte-sized locations that accessed via a memory address. In the program below, we demonstrate how to display the address of a char-type variable. First, we use the "address of" operator, & to get a char pointer to the variable. Then we cast the char pointer to an int pointer, (int*), so that the memory address is displayed. We need this cast because char pointers are are treated like char strings by the output stream cout.

#include <iostream>

int main()
{
	using namespace std;

	char cMyChar = 'x';
	cout << "Address: " << (int*)(&cMyChar) << endl;

	return 0;
}

Executing this program, by pressing (CTRL + F5), outputs the address like this:

Output1

The address in the previous program contains letters because it is written in hexadecimal. We will always use hexadecimal for memory addresses to avoid confusion with other integers. However, if you would like to see the address in decimal, you can cast it to an int type, (int), as we do in the program below.

#include <iostream>

int main()
{
	using namespace std;

	char cMyChar = 'x';
	cout << "Address (Hex): " << (int*)(&cMyChar) << endl;
	cout << "Address (Dec): " << (int)(&cMyChar) << endl;

	return 0;
}

Again, executing this program, by pressing (CTRL + F5), outputs the addresses in hexadecimal and decimal like this:

Output2

For our third and final program, we create an array of chars. In fact, the array is a null-terminated character array string. Starting in the main() function, we declare ar array of chars, called caArray and set it equal to "XoaX.net." This creates an array of nine characters, including a null-terminator, which is added to the end.

After this, we run a loop over the entries of the array and output the address, followed by the representation of each char as a binanry number, an ASCII character, and as a decimal number. The computer stores the binary number in memory. However, we will most often see the ASCII character value printed out, as we do here by default. ASCII stands for American Standard Code for Information Interchange and all of ASCII values can be found in ASCII table in our reference section. Notice that we had to write a function to output the binary representation. This is because binary is seldom used when we want to display values.

#include <iostream>

void OutputBits(char cByte) {
	using namespace std;
	unsigned int uiByte = cByte;
	unsigned int uiMask = 128;
	for (int iIndex = 0; iIndex < 8; ++iIndex) {
		if (((uiMask >> iIndex) & uiByte) != 0) {
			cout << "1";
		} else {
			cout << "0";
		}
	}
}

int main() {
	using namespace std;

	char caArray[] = "XoaX.net";
	// Address, Bits, Char, Decimal
	for (int iIndex = 0; iIndex < 9; ++iIndex) {
		cout << "Address: " << (int*)(&(caArray[iIndex])) <<
			"  Bits: ";
		OutputBits(caArray[iIndex]);
		cout << "  Char: " << caArray[iIndex] <<
			"  Decimal: " << (int)(caArray[iIndex]) << endl;
	}

	return 0;
}

Compiling and executing this program, by pressing (CTRL + F5), prints out a table of addresses, followed by the binary, ASCII, and decimal representations of the char value:

Output3