C - Program - 2e - Chapter05.pdf
(
370 KB
)
Pobierz
Functions
Part 2:Variable
Addresses, Pointers,
and References
5
CHAPTER OBJECTIVES
Demonstrate how data variables and computer memory are
related.
KEY TERMS AND CONCEPTS
assigning address into a pointer
hex address
hexadecimal notation
memory
memory efficient programming
pointer
pointer declaration
references
stack
call-by-reference with pointers
call-by-reference with reference
parameters
Introduce the address operator, which returns a variable’s hex
address.
Illustrate the concept of a pointer, which is a variable that
holds another variable’s address.
Show how the pointer can be used to access another vari-
able’s contents with the indirection operator.
Introduce the C++ reference variable.
Present an overview of how pointers and references are used
in C++ programs and why they are important.
KEYWORDS AND OPERATORS
address operator,
&
indirection operator,
*
reference operator,
&
sizeof
Explain when it is necessary to use pointers and references with
functions.
Show the correct use of pointers and references in functions.
We’re Good Up To Here.
Y
ou have been introduced to a lot of C++ topics in the past four chapters.
You are becoming better at using the C++ string, stringstream, and vector
classes, making objects and using them to call the class functions. You are
writing your own functions, and even splitting your source code into multiple files.
Excellent! However, we are missing one tool in our function-writing toolkit. What
could that be, you wonder? Here is a question for you. What if you need to return
more than one data item from a function? For example, you wish to write a func-
tion that asks the user to input the dimensions of a box, length, width, and height.
Wouldn’t it be better to just have one function that obtains these three items in-
stead of writing three functions, or (horrors!) using global variables?
This chapter fills in the missing pieces for us regarding “returning” several
items from a function. We have to delve deeper into how variables are stored in
memory in order to understand the two new tools we’re going to be using. We’ll
learn the older, C-style technique that uses “pointers,” and then see how to work
with C++ references. Programmers may argue that we should always use refer-
ences because they are easier, but if you progress to developing more ad-
vanced C++ problems or writing graphic user interface programs (such as
wxWidgets code) you’ll need to be an expert with pointers and references. We’ll
cover both here so you’ll be ready for anything in the future.
5.1
Data Variables and Memory
We need to cover some background material here to fully understand the core
topics in this chapter. When data variables are declared in a program (such as
int x;
), physical memory in the random access memory (RAM) is reserved for
the variable. The number of memory bytes that are actually used depends on the
data type of the variable. Table 5-1 (a duplication of Table 2-6 in Chapter 2,
page 23) lists all the data types, the bytes reserved, and ranges as dictated by the
ISO C++ Standard. (We’ll need to have this table handy as we progress through
this chapter.)
245
246
Chapter 5
❚
Functions Part 2: Variable Addresses, Pointers, and References
❚
TABLE 5-1
Data Types Defined by the ANSI/ISO C Standard
Keyword
Typical Bytes of Memory
Precision Range
char
1
-
128
to 127
unsigned char
1
0 to 255
signed char
1
-
128
to 127
int
4
-
2,147,483,648
to 2,147,483,647
short int
2
-
32,768
to 32,767
unsigned short int
2
0 to 65,535
unsigned int
4
0 to 4,294,967,295
long int
4
-
2,147,483,648
to 2,147,483,647
unsigned long int
4
0 to 4,294,967,295
float
4
6 digits, i.e., 0.xxxxxx
(7 digits in Visual C++)
3.4 E
;
38
double
8
10 digits, i.e., 0.xxxxxxxxxx
(15 digits in Visual C++)
1.7 E
;
308
long double
10
10 digits, i.e., 0.xxxxxxxxxx
(19 digits in Visual C++)
1.2 E
;
4932
sizeof
Operator
C++ provides the
sizeof
operator, which returns the number of bytes reserved for
either a data type or a variable. The form for the
sizeof
operator is:
sizeof
an operator that re-
turns the number of
bytes reserved for
the variable or for
data type
sizeof variable_name;
//for a variable value () optional, but convenient
sizeof (data_type);
//for a data type (must be in parentheses)
In the short program of Program 5-1, we use the
sizeof
operator with the variable
name to determine the bytes of memory reserved for five popular data types.
Compare the results with the values shown in Table 5-1.
Program 5-1
1 //A program that demonstrates the sizeof operator.
2
3 #include <iostream>
4 using namespace std;
5
6 int main()
7 {
8
char c;
9
double d;
10
float f;
11
int i;
12
long l;
13
14
//Use sizeof with data types
247
Section 5.1
❚
Data Variables and Memory
15
cout << "\n The sizeof Demonstration Program.";
16
cout << "\n Number of bytes reserved in RAM for:"
17
<< "\n char " << sizeof(c)
18
<< "\n double " << sizeof(d)
19
<< "\n float " << sizeof(f)
20
<< "\n int " << sizeof(i)
21
<< "\n long " << sizeof(l) << endl;
22
23
return 0;
24 }
Output
The sizeof Demonstration Program.
Number of bytes reserved in RAM for:
char 1
double 8
float 4
int 4
long 4.
Reserving Memory
In the following three declaration statements, memory space is reserved for these
six variables—4 bytes of memory for each
float
and
int
, and 8 bytes for each
double
.
float x,y;
//4 bytes reserved for each float and int
int i,j;
double q,r;
//8 bytes reserved for each double
The computer reserves actual memory locations for program variables. If the vari-
ables are declared inside a function (i.e., local variable), they are placed on the
stack
.The stack is a region of memory reserved for program variables. The first
variable that is declared is placed at the far end of the stack. Data variables are
stacked into memory, and the first variable—because it is located at the end of the
stack—has the highest address. We will not worry about the stack, but when the
memory locations are examined, we see that the last declared variable (in this case
“
r
”) has the lowest memory address. Figure 5-1 illustrates how the six variables in
the declaration statements above are reserved in memory. The boxes in Figure 5-1
represent the memory for each variable.
stack
the portion of the
computer memory
where local program
variables are stored
hexadecimal
notation
the notation for
showing how com-
puter memory is
referenced
Computer Memory and Hex
Computer memory is addressed by using
hexadecimal notation
.(For a discussion
of hexadecimal notation, see Appendix E, “Bits, Bytes, Memory, and Hexadecimal
Notation.”) In a 32-bit environment, the addresses are 4 bytes long. A memory lo-
cation might have the
hex address
of 0x0066FDF0.
hex address
the address of a
memory location of
a variable
248
Chapter 5
❚
Functions Part 2: Variable Addresses, Pointers, and References
Type:
Name:
double
r
double
q
int
j
int
i
float
y
float
x
float x, y;
int i, j;
double q, r;
Bytes of memory:
8
8
4
4
4
4
RAM stack
Low end of the stack
Last declared variable will
have lowest memory address.
r
q
j
i
y
x
Far end of the stack
Figure 5-1
Memory reserved for local data variables is placed on the stack.
In Figure 5-2, the six variables in the declaration statements above are shown
with memory addresses. This diagram assumes that the memory address for the
first byte of variable “
r
” is located at 0x0066FDD8. Each of the six variable ad-
dresses is shown.
If we assign values to the six variables, as shown in the code below, each value
is stored in memory. In Figure 5-3 the variable values are written in the boxes.
Floating point variables have six digits of precision, doubles have at least ten, and
integers are whole numbers.
Type:
Name:
double
r
double
q
int
j
int
i
float
y
float
x
D8 D9 DA DB DC DD DE DF
E0 E1 E2 E3 E4 E5 E6 E7
E8 E9 EA EB
EC ED EE EF
F0 F1 F2 F3
F4 F5 F6 F7
Address of first byte:
Figure 5-2
Data variables and their hexadecimal addresses.
Plik z chomika:
Januszek66
Inne pliki z tego folderu:
Back Seat_ A Mumbai Tale - Aditya Kripalani.mobi
(755 KB)
Brief Wondrous Life of Oscar Wao, The - Junot Diaz.opf
(3 KB)
Don't Make Me Think, Revisited_ - Steve Krug.mobi
(9256 KB)
M. T. Anderson - Norumbegan 03 - The Empire of Gut and Bone # (v5.0).epub
(2209 KB)
M. T. Anderson - Norumbegan 02 - The Suburb Beyond the Stars # (v5.0).epub
(2105 KB)
Inne foldery tego chomika:
Dokumenty
Galeria
LUDLUM ROBERT
Midi - Kar
Mszał Rzymski PL
Zgłoś jeśli
naruszono regulamin