CSCI 261 Programming Concepts (C++)

Winter/Spring 2012

Homework 31: Addresses and Value Storage

Right-click me and select "Save target as..." to save.

Concepts

For this assignment, focus on using the reference ("address of") operator.

The Importance of Understanding Memory

One benefit of using higher level languages is that they free the programmer from needing to worry about telling the machine how to store and manage memory. C++ is a high level language, but it also provides us the ability to inspect and manage memory directly, via pointers and references. Being able to manage the storage of values in memory is a critical requirement for more advanced programs (such as those you will see in CSCI 262).

More importantly, however, it is critical to know how our programs interact with memory.

"Why?" you ask, "I can write useful programs and not care about memory addresses and how they work!" This is very true. But consider your fancy car that you might need to take to a mechanic. What if that mechanic told you, "I have no idea how cars work, but I sure can fix yours!" How much trust would you put in that mechanic? As software engineers, we must obtain a deeper understanding of how our programs work in order to be most effective and to eventually create more powerful programs (just as a mechanic needs to know how the car works in order to, say, build his/her own supercar).

Values and Memory

In class we demonstrated the ideas of the stack and the heap and how data (values named by variables) are stored in memory. To help illustrate this, we will start with learning how to use the & operator, which is a little tricky because it tends to mean something different depending on the context. For example, you have seen this &operator in the context of function definitions:

void battle(Troll &left, Troll &right);

In this context, we're telling the compiler that "battle is a function that accepts two trolls by reference." To elaborate, we're really stating that this function to not pass a copy of two trolls when called; rather, it should expect to receive two references to two Troll objects that are defined by the caller.

In another, more pure, context, you use the & operator to obtain the memory address of where something is stored. For example:

Troll fred;
cout << &fred;

On the first line above, we're saying that fred is a Troll object. On the second line, we're using the & operator to say, "give me the memory address where the Troll fred is stored, and print that address." Simple!

In a nutshell, you should realize that & is an unary operator that simply returns the memory address of its operand.

Instructions

For this assignment, we'd like you to simply apply the & operator to illustrate the memory addresses of things stored in memory, and to gain an understanding of hexadecimal representation of numbers. In class you saw how hex numbers are really just shorthand for their binary equivalents. More importantly, when completing this assignment you should read the addresses that are printed to the screen, and notice their relationship to one another.

Write a program that:

When run, your program should display something that looks like this:

The value of myStatic is 42 and its address is 0x100000e90
The value of name is Marley and its address is 0x7fff5fbff078
The value of x is 100 and its address is 0x7fff5fbff084
The value of y is 36 and its address is 0x7fff5fbff070
The Dog variable is stored at address 0x7fff5fbff096

Note that your addresses will definitely be different. Your variable names and their values are up to you.

While the code for this assignment is rather trivial, you should ask yourself:

Lastly, realize this is a syntax exercise to prepare you for our upcoming assignment with pointers.

Requirements and Rubric

A friendly message from The Terminator, our grading program.

Hello *bzzzt* again. I can't wait for school to be over, can *bzzzzt* you? I will check for the following:


Your program must print the addresses of five different values to the screen.

Your program must use the & operator to print these memory addresses.


It's getting warmer, I need *bzzzt* to find my robot flip-flops (for my feet, not my logic gates).

This work is worth 110 points.

Requirement Points Notes
Place your name in the comment header in main.cpp 5
Correct submission as .zip file 5
Correctly prints the address of five variables 100 (20 each)

Concepts Exercised: classes, OOP, memory, datatypes