chap03.htm

来自「Since the field of object oriented progr」· HTM 代码 · 共 256 行

HTM
256
字号
<HTML>
<HEAD>
   <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
   <META NAME="Author" CONTENT="Gordon Dodrill">
   <META NAME="GENERATOR" CONTENT="Mozilla/4.04 [en] (Win95; I) [Netscape]">
   <TITLE>C++ Tutorial - Chapter 3</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<B>C++ Tutorial - Chapter 3</B>

<P><B><FONT SIZE=+3>P</FONT><FONT SIZE=+2>OINTERS</FONT></B>

<P>Because pointers are so important in C and C++, this chapter will review
some of the more important topics concerning pointers. Even if you are
extremely conversant in the use of pointers, you should not completely
ignore this chapter because some new material unique to C++ is presented
here.

<P><B>POINTER REVIEW</B>

<P>Example program ------> <B><A HREF="POINTERS.CPP">POINTERS.CPP</A></B>

<P>Examine the program named POINTERS.CPP for a simple example of the use
of pointers. This is a pointer review and if you are comfortable with the
use of pointers, you can skip this example program completely.

<P>A pointer in either ANSI-C or C++ is declared with an asterisk preceding
the variable name. The pointer is then a pointer to a variable of that
one specific type and should not be used with variables of other types.
Thus <B>pt_int</B> is a pointer to an integer type variable and should
not be used with any other type. Of course, an experienced C programmer
knows that it is simple to coerce the pointer to be used with some other
type by using a cast, but he must then assume the responsibility for its
correct usage.

<P><IMG SRC="CPP0301.GIF" HSPACE=20 VSPACE=20 BORDER=0 HEIGHT=166 WIDTH=475>

<P>In line 12 the pointer named <B>pt_int</B> is assigned the address of
the variable named <B>pig </B>and line 13 uses the pointer named<B> pt_int</B>
to add the value of <B>dog </B>to the value of <B>pig </B>because the asterisk
dereferences the pointer in exactly the same manner as standard C. Figure
3-1 is a graphical representation of the data space following execution
of line 13. Note that a box containing a dot represents a pointer. The
address is used to print out the value of the variable <B>pig </B>in line
14 illustrating the use of a pointer with the output stream object <B>cout</B>.
Likewise, the pointer to <B>float </B>named <B>pt_float</B> is assigned
the address of <B>x</B>, then used in a trivial calculation in line 18.

<P><B>CONSTANT POINTERS AND POINTERS TO CONSTANTS</B>

<P>The definition of C++ allows a pointer to a constant to be defined such
that the value to which the pointer points cannot be changed but the pointer
itself can be moved to another variable or constant. The method of defining
a pointer to a constant is illustrated in line 22. In addition to a pointer
to a constant, you can also declare a constant pointer, one that cannot
be changed. Line 23 illustrates this. Note that neither of these pointers
are used in illustrative code.

<P>Either of these constructs can be used to provide additional compile
time checking and improve the quality of your code. If you know a pointer
will never be moved due to its nature, you should define it as a constant
pointer. If you know that a value will not be changed, it can be defined
as a constant and the compiler will tell you if you ever inadvertently
attempt to change it.

<P><B>A POINTER TO VOID</B>

<P>The pointer to <B>void </B>is actually a part of the ANSI-C standard
but is relatively new so it is commented upon here. A pointer to <B>void
</B>can be assigned the value of any other pointer type. You will notice
that the pointer to <B>void </B>named <B>general </B>is assigned an address
of an <B>int </B>type in line 15 and the address of a <B>float </B>type
in line 20 with no cast and no complaints from the compiler. This is a
relatively new concept in C and C++. It allows a programmer to define a
pointer that can be used to point to many different kinds of things to
transfer information around within a program. A good example is the <B>malloc()</B>
function which returns a pointer to <B>void</B>. This pointer can be assigned
to point to any entity, thus transferring the returned pointer to the correct
type.

<P>A pointer to <B>void </B>is aligned in memory in such a way that it
can be used with any of the simple predefined types available in C++, or
in ANSI-C for that matter. They will also align with any compound types
the user can define since compound types are composed of the simpler types.

<P>If you are not completely comfortable with this trivial program using
pointers, you should review the use of pointers in any good C programming
book or Coronado Enterprises C tutorial before proceeding on because we
will assume that you have a thorough knowledge of pointers throughout the
remainder of this tutorial. It is not possible to write a C program of
any significant size or complexity without the use of pointers.

<P>Be sure to compile and execute this program.

<P><B>DYNAMIC ALLOCATION AND DEALLOCATION</B>

<P>Example program ------> <B><A HREF="NEWDEL.CPP">NEWDEL.CPP</A></B>

<P>Examine the program named NEWDEL.CPP for our first example of the <B>new
</B>and <B>delete </B>operators. The <B>new </B>and <B>delete </B>operators
do dynamic allocation and deallocation in much the same manner that the
<B>malloc()</B> and <B>free() </B>functions do in your old favorite C implementation.

<P>During the design of C++, it was felt that since dynamic allocation
and deallocation are such a heavily used part of the C programming language
and would also be heavily used in C++, it should be a part of the language,
rather than a library add-on. The <B>new </B>and <B>delete </B>operators
are actually a part of the C++ language and are operators, much like the
addition operator or the assignment operator. They are therefore very efficient,
and are very easy to use as we will see in this example program.

<P>Lines 15 and 16 illustrate the use of pointers in the tradition of C
and line 17 illustrates the use of the <B>new </B>operator. This operator
requires one modifier which must be a type as illustrated here. The pointer
named <B>point2 </B>is now pointing at the dynamically allocated integer
variable which exists on the heap, and can be used in the same way that
any dynamically allocated variable is used in ANSI-C. Lines 19 and 20 illustrate
displaying the value on the monitor which was assigned in line 18.

<P>Line 21 allocates another new variable and line 22 causes <B>point2
</B>to refer to the same dynamically allocated variable as <B>point1 </B>is
pointing to. In this case, the reference to the variable that <B>point2
</B>was previously pointing to has been lost and it can never be used or
deallocated. It is lost on the heap until we return to the operating system
when it will be reclaimed for further use, so this is obviously not good
practice. Note that <B>point1 </B>is deallocated with the <B>delete </B>operator
in line 26, and <B>point2 </B>can not be deleted since it is now pointing
to nothing. Since the pointer <B>point1 </B>itself is not changed, it is
probably still pointing to the original data on the heap. This data could
probably be referred to again using <B>point1</B>, but it would be terrible
programming practice since you have no guarantee what the system will do
with the pointer or the data. The data storage is returned to the free
list to be allocated in a subsequent call, and will soon be reused in any
practical program.

<P>Since the <B>delete </B>operator is defined to do nothing if it is passed
a NULL value, it is legal to ask the system to delete the data pointed
to by a pointer with the value of NULL, but nothing will actually happen.
It is actually wasted code. The <B>delete </B>operator can only be used
to delete data allocated by a <B>new </B>operator. If the <B>delete </B>is
used with any other kind of data, the operation is undefined and anything
can happen. According to the ANSI standard, even a system crash is a legal
result of this illegal operation, and can be defined as such by the compiler
writer.

<P>In line 28, we declare some floating point variables. You will remember
that in C++ the variables do not have to be declared at the beginning of
a block. A declaration is an executable statement and can therefore appear
anywhere in a list of executable statements. One of the <B>float </B>variables
is allocated within the declaration to illustrate that this can be done.
Some of the same operations are performed on these <B>float </B>type variables
as were done on the <B>int </B>types earlier.

<P>Some examples of the use of a structure are given in lines 36 through
44 and should be self explanatory.

<P>Finally, since the <B>new </B>operator requires a type to determine
the size of the dynamically allocated block, you may wonder how you can
allocate a block of arbitrary size. This is possible by using the construct
illustrated in line 48 where a block of 37 <B>char </B>sized entities,
which will be 37 bytes, is allocated. A block of 133 bytes greater than
the size of the date structure is allocated in line 50. It is therefore
clear that the <B>new </B>operator can be used with all of the flexibility
of the <B>malloc()</B> function which you are familiar with. The brackets
are required in lines 49 and 51 to tell the compiler that it is deallocating
an array.

<P>The standard functions which you have been using in C for dynamic memory
management, <B>malloc()</B>, <B>calloc()</B>, and <B>free()</B>, are also
available for use in C++ and can be used in the same manner they were used
in C. If you are updating code with the older function calls, continue
to use them for any additions to the code. If you are designing and coding
a new program you should use the newer constructs because they are a built
in part of the language rather than an add on and are therefore more efficient.
It is an error to <B>delete </B>a variable that has been <B>malloc</B>'ed
and it is an error to <B>free </B>a variable that was allocated with <B>new</B>.

<P>Be sure to compile and execute this program.

<P><B>POINTERS TO FUNCTIONS</B>

<P>Example program ------> <B><A HREF="FUNCPNT.CPP">FUNCPNT.CPP</A></B>

<P>Examine the program named FUNCPNT.CPP for an example of using a pointer
to a function. It must be pointed out that there is nothing new here, the
pointer to a function is available in ANSI-C as well as in C++ and works
in the manner described here for both languages. It is not regularly used
by most C programmers, so it is briefly discussed here as a refresher.
If you are comfortable with the use of pointers to functions, you can skip
this discussion entirely.

<P>There is nothing unusual about this program except for the pointer to
a function declared in line 7. This declares a pointer to a function which
returns nothing (<B>void</B>) and requires a single formal parameter, a
<B>float </B>type variable. You will notice that all three of the functions
declared in lines 4 through 6 fit this profile and are therefore candidates
to be called with this pointer. If you have not used prototyping in C,
these lines will look strange to you. Don't worry about them at this point
since we will study prototyping in the next chapter of this tutorial.

<P>Observe that in line 14 we call the function <B>print_stuff()</B> with
the parameter <B>pi </B>and in line 15 we assign the function pointer named
<B>function_pointer</B> the value of <B>print_stuff</B> and use the function
pointer to call the same function again in line 16. Lines 14 and 16 are
therefore identical in what is accomplished because of the pointer assignment
in line 15. In lines 17 through 22, a few more illustrations of the use
of the function pointer are given. You will be left to study these on your
own.

<P>Since we assigned the name of a function to a function pointer, and
did not get an assignment error, the name of a function must be a pointer
to that function. This is exactly the case. A function name is a pointer
to that function, but it is a pointer constant and cannot be changed. This
is exactly the case we found when we studied arrays in ANSI-C at some point
in our C programming background. An array name is a constant pointer to
the first element of the array.

<P>Since the name of the function is a constant pointer to that function,
we can assign the name of the function to a function pointer and use the
function pointer to call the function. The only caveat is that the return
value and the number and types of parameters must be identical. Most C
and C++ compilers will not, and in fact, can not warn you of type mismatches
between the parameter lists when the assignments are made. This is because
the assignments are done at runtime when no type information is available
to the system, rather than at compile time when all type information is
available.

<P>The use and operation of pointers must be thoroughly understood when
we get to the material on dynamic binding and polymorphism later in this
tutorial. It will be discussed in detail at that time.

<P>Be sure to compile and execute this program.

<P><B>PROGRAMMING EXERCISES</B>
<OL>
<LI>
When dynamically allocated data is deleted, it is still actually in memory,
stored on the heap. Repeat the output statement from lines 24 and 25 of
NEWDEL.CPP immediately following the delete in line 26 to see if the values
are really still there. Repeat it once again just prior to the end of the
program when the data spaces should have been written over to see if you
get garbage out. Even if your compiler reports the correct data, it is
terrible practice to count on this data still being there because in a
large dynamic program, the heap space will be used repeatedly.</LI>

<LI>
Add a function to FUNCPNT.CPP which uses a single integer for a parameter
and attempt to call it by using the function pointer to see if you get
the correct data into the function.</LI>
</OL>
<A HREF="chap04.htm">Advance to Chapter 4</A>

<P><A HREF="cpplist.htm">Return to the Table of Contents</A>
</BODY>
</HTML>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?