Held Wednesday, February 9, 2000
Today we consider one of the most important features of
C, pointers. Pointers provide power to your programs, but
inappropriate use of points can destroy programs.
- If you want more information on pointers and memory, it might
help to read the Stanford CS Library's documentation on pointers
and memory. I've made
a local copy.
- As some of you have learned (or already know), I get much too much
email. This means that I tend to leave less crucial email until
later. In particular, I typically ignore emailed assignments until
I grade them. Hence, if you send in a question on an assignment
the same day an assignment is due, I will not read it until much
later. Please start messages you want me to read with something
like "READ ME NOW!" or "IMPORTANT" or "HELP".
- Are there questions on homework
- Pointers and addresses
- Pointers in various langauges
- Pointer pitfalls
- By this point in your careers, you should be fairly comfortable
with the notion of variables.
- From my perspective, variables are containers that store
- What kinds of values can they store? At the most basic level,
they can store values of the primitive types, such as
- In some languages, variables can store the address of another
container. We call such addresses pointers
- Why is it useful to store addresses?
- It provides a form of generality. If we have many containers that
we want to change, we can put each address in turn into the same
container and then affect them indirectly.
- It lets us dynamically allocate memory. You can't (easily) implement
dynamic data structures without some form of pointer.
- We often draw pictures, but we may also describe pointers numerically.
- In order to understand pointers, it may help to look ``behind the
scenes'' in the implementation of a typical programming
- In practice, the containers we talked about before are just
areas of memory.
- A symbol table maps every variable name (and parameter)
to a corresponding address in memory.
- In Java, every variable that contains objects is really a
pointer variable that points to the object.
- That's why we have all the fun aliasing problems.
- In C, you must explicitly indicate that something is a
- Given a type,
t, you declare a variable that
points to containers of type
- For example, here's how you say ``
ip points to an
- What can you do with pointers?
- You can copy them with the assignment operator,
ip = jp;
- You can determine the contents of the container they point to
- You can change those contents, too
- You can assign the address of another container to them. You
get the address of a variable with
- You can allocate new memory and get a pointer to it with
ip = (int *) malloc(sizeof(int));
- You can pass them as parameters:
*ip = 5;
- You can move to the next chunk of memory with
Thursday, 20 January 2000
- Created as a blank outline.
Wednesday, 9 February 2000
Friday, 11 February 2000
- Moved uncovered topics to the next outline.
Back to Headers and Makefiles.
On to Memory Management.