UMBC CMSC202, Computer Science II, Spring 1998,
Sections 0101, 0102, 0103, 0104 and Honors
Tuesday March 3, 1998
Assigned Reading:
- A Book on C: 6.10 - 6.14
- Programming Abstractions in C: 2.7
- Understanding Pointers in C: Chapters 1-3
Handouts (available on-line):
none (material described in syllabus is superseded by "Understanding
Pointers in C")
Topics Covered:
- Pointers and types. The following sequence of examples
demonstrate that a pointer isn't simply an address. A pointer is
an address of a particular type.
- In our first program we have
a pointer to an array of type int_array and a pointer
to an integer. Note that when the two pointers are printed
out, they have the same address. However, since the two
pointers have different types, we need different mechanisms
to access the first element of the array. (See
sample run.)
- Our second program and
sample run shows that if we
reversed the array access mechanisms for the two pointers
in the first program, the compiler complains that the
mechanisms are inappropriate for the types of the pointers.
- The third program shows
another situation where pointers can have the same address
but different types. Here we have a record whose first
field is an array of integers. So, we can have a pointer
to the record, a pointer to the first field of the record
and a pointer to the first element of the array in the
first field of the record. All three pointers have the
same address, but different types. (See
sample run.)
- Our fourth program shows
yet another situation where the type of a pointer is important:
pointer arithmetic. We have two pointers. The first one is
a pointer to an array of 17 integers. The second one is a pointer
to an integer. If we increment both pointers, the first pointer's
value is increased by 68 (= 4 * 17) and the second pointer's value
is increased by 4. Recall that the size of an int variable on this
system is 4. Increasing the integer pointer's address by 4 is useful
for array access. (See sample run.)
- Memory allocation. The next four programs demonstrate the
use of the malloc() function.
- Program 1 shows what might happen
if you store a value in the location given by an uninitialized
pointer. The program crashes with a "Bus Error". We can use
the debugger to figure out where the error occurred.
(See sample run.)
- Program 2 demonstrates the
use of malloc() and calloc() to allocate memory
for an array of integers. You should always check if the return
value from malloc() and calloc() are NULL. The
system may have run out of memory. (See sample
run.)
- Program 3 shows what can happen if
you overrun the end of an array. In this case, we overwrite some
information used by malloc()that is stored immediately after
the end of the array of doubles. As a consequence, the program
crashes during the next call to calloc(). In a situation
like this, it is tempting to think that you have discovered a bug
in the system software. However, it is much more likely that you
have a bug in your program. The difficulty in tracing this error is
that the program crashes after the mistake (overrunning the array)
has been made. Thus, place where the program can crashes be very
far from the location of the bug. The upshot is that you should
be very careful with your for loops when you work with dynamically
allocated arrays. (See sample run.)
- Program 4 is a simple example
which shows that malloc() might return a NULL value
if you request too much memory. (See sample
run.)
- We briefly looked at how to use the realloc() function.
This will be discussed again in the next lecture.
Program and sample
run.
Last Modified:
22 Jul 2024 11:27:45 EDT
by
Richard Chang
Back up
to Spring 1998 CMSC 202 Section Homepage