| Name: _____________________ | Class: Comp 217 |
| SSN/ID: _____________________ | Section & Group: ____________ |
"The name of the song is called "Haddocks' Eyes"."
"Oh, that's the name of the song, is it?" Alice said, trying to feel interested.
"No, you don't understand," the Knight said, looking a little vexed.
"That's what the name is called. The name really is "The Aged Aged Man"."
"Then I ought to have said "That's what the song is called"?" Alice corrected herself.
"No, you oughtn't: that's quite another thing! The song is called "Ways and Means":
but that's only what it's called, you know!"
"Well, what is the song, then"" said Alice, who was by this time completely bewildered.
"I was coming to that," the Knight said. "The song really is "A-sitting On a Gate":
and the tune's my own invention."
Lewis Carroll, Through The Looking-Glass
|
A variable data object (i.e., a normal variable) can be thought of as having three components:
Getting Started
/************************************************************
* Name:
* Lab #:
* Date:
************************************************************/
#include <iostream>
using namespace std;
int main() {
short int si1, si2;
return 0;
}
Addresses
Note: Addresses may not display correctly on your system.
If necessary, use a typecast to make them "typeless" before
outputting them, as in (void *) &variable. You can either
cast it to a void pointer or an unsigned long. For example:
void can be used to mean nothing and anything (see this great tutorial (http://vergil.chemistry.gatech.edu/resources/programming/c-tutorial/pointers.html) or this nice tutorial: http://www.augustcouncil.com/~tgibson/tutorial/ptr.html).
si1: ________________ si2: ________________
Memory Maps
long intVal;
char ch;
it allocates memory for the variables ch and
intVal. If the memory locations allocated to
intVal are 0x09 through 0x0C (in
hexadecimal), and the compiler allocates adjacent memory locations
to ch and intVal (from higher to lower
addresses), we might picture memory as follows:
| Name | ch | intVal | |||||
|---|---|---|---|---|---|---|---|
| Value | |||||||
| Address | ... | 0x08 | 0x09 | 0x0A | 0x0B | 0x0C | ... |
The memory address associated with intVal would be 0x09, even though it actually consists of locations 0x09 through 0x0C, as indicated by the shaded part of the picture.
si1: ________________ si2: ________________
i1: ________________ i2: ________________
li1: ________________ li2: ________________
f1: ________________ f2: ________________
d1: ________________ d2: ________________
ld1: ________________ ld2: ________________
b1: ________________ b2: ________________
c1: ________________ c2: ________________
(If the character address doesn't display correctly, please
typecast it to an (unsigned) or (void
*), as in the earlier Note. This is because the cout stream
assumes that you are passing a pointer to a character, and you
must want to print a string. Therefore, you have to tell it
otherwise by casting it to a void pointer.)
Sketch a memory map of the memory allocated to one or two of these
variables (you may use several "strips" of memory if necessary,
but be sure to show addresses and variable names):
Note: Addresses are typically displayed in
hexadecimal (base 16) notation, so you may have to do some
hexadecimal-to-decimal conversion or do your calculations using
hexadecimal arithmetic. (You might be able to have the program do
this for you -- try casting the addresses to type unsigned.)
short int: ________________ int: ________________ long int: ________________
float: ________________ double: ________________ long double: ________________
bool: ________________ char: ________________
The sizeof operator, which has two common forms,
Dereferencing
The value stored in a memory location can be accessed using the
dereferencing operator *. Show this by putting the following
statements in your program (see the earlier note on void *).
si1 = 12345;
cout << "si1 = " << si1 << endl
<< "Address of si1 = " << &si1 << endl
<< "Contents of " << &si1 << " = " << *(&si1) << endl;
Hand In: This lab handout with the answers filled in attached
to a listing of your final program
(use the enscript command from your Programming Style Sheet to print it out:
enscript -E -G -2rj -M Letter -PECT2_PS <filename> or a2ps <filename>).