He’s the one Who likes all our pretty songs And he likes to sing along And he likes to shoot his gun But he don’t know what it means.
—Nirvana, “In Bloom”
Like a song about music, or a movie about Hollywood, a pointer is data
describing other data. It’s certainly easy to get overwhelmed: all at once,
you have to deal with getting lost in references to references, aliases,
memory management, and
malloc. But our
outrageous fortune breaks down into separate components. For example, we can
use pointers as aliases without bothering with
malloc, which doesn’t have to appear nearly as
often as the textbooks from the ’90s told us it did. On the one hand, C’s
syntax can be confusing with its use of stars; on the other hand, C’s syntax
provides us with tools for dealing with especially complicated pointer
setups like pointers to functions.
C provides three basic models of memory management, which is two more than most languages and two more than you really want to care about. And for you, dear reader, I’ll even throw in two—yes, two—bonus memory models later on in Chapter 12.
You declare a variable on first use, and it is removed when it
goes out of scope. Without the
static keyword, any variable inside a
function is automatic. Your typical programming language has only
Static variables exist in the same place throughout the life of the program. Array sizes are fixed at startup, but values ...