UMBC CMSC202, Computer Science II, Fall 1998,
Sections 0101, 0102, 0103, 0104
1. Introduction
Tuesday September 01, 1998
[Next Lecture]
Assigned Reading: 1.1 - 1.16
Handouts (available on-line):
Programs from this lecture.
Topics Covered:
- Discussed prerequisites and goals of this course.
- Pointed out the attendance policy.
- Made special note of the cheating policy.
- Discussed why you are better off if this course is
hard rather than easy.
- We covered an example of a naming problem in C. In this
example, we have a simple interface for a function called
StringLength(). (See header
file and implementation.)
- In the straightforward use of the StringLength()
function, the main program
simply includes the header file slength.h and calls
StringLength() as usual. The sample run shows nothing unusual.
- In the next example, the main
program happens to include a global variable called
strlen. This creates a naming conflict because
strlen is of course the name of the standard function in
the string.h library that StringLength() uses to
find the length of the string. One sample run on an SGI Indigo running
the IRIX 5.3 operating system shows that the compiler gave no
warning and no errors for this naming conflict. Running the
compiled program causes a core dump. The next sample run is produced on an SGI
running IRIX 6.2. This version of the compiler at least gives some
warning about the naming conflict. Nevertheless, the compiler
produces an executable file which dumps core.
- Our next main program
and sample run shows that one
way we can resolve the naming conflict is to make the
strlen global variable a private variable. This
is done using the keyword static in the declaration
of the global variable strlen.
- One response to the example above is that you shouldn't use
global variables in the first place. This is not a valid point
because the same problem occurs if you have a function called
strlen. In the next example, the
main program uses a function called strlen() to
store the length of a string. The sample
run shows that we have the same naming problem as before.
In this example, however, compiling the same programs using C++
removes the problem entirely. This is because the C++ compiler
can tell the difference between the two functions called
strlen(). We will discuss this point in greater detail
when we talk about function overloading.
- In C, we can also use the keyword staticto make
a function private. (See program
and sample run.)
-
Since it is unreasonable to require that programmers know the names
of all functions that might eventually be linked into their
programs, it is best to adopt the strategy that one should make
private those functions and global variables that are not to be used
by other functions. We will also see throughout the semester that the
C++ language has many mechanisms (perhaps too many) to allow and deny
access to functions and variables from different parts of a program.
[Next Lecture]
Last Modified:
22 Jul 2024 11:28:49 EDT
by
Richard Chang
Back up
to Fall 1998 CMSC 202 Section Homepage