Chapter 6. Your Pal the Pointer
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.
Automatic, Static, and Manual Memory
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.
- Automatic
You declare a variable on first use, and it is removed when it goes out of scope. Without the
statickeyword, any variable inside a function is automatic. Your typical programming language has only automatic-type data.- Static
Static variables exist in the same place throughout the life of the program. Array sizes are fixed at startup, but values ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access