C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1
in this book has used the heap.

Thinking of the Heap


Now that you’ve learned what the heap is—the unused section of contiguous memory—throw out
what you’ve learned! You’ll more quickly grasp how to use the heap if you think of the heap as just
one big heap of free memory stacked up in a pile. The next paragraph explains why.


You’ll be allocating (using) and deallocating (freeing back up) heap memory as your program runs.
When you request heap memory, you don’t know exactly from where on the heap the new memory
will come. Therefore, if one statement in your program grabs heap memory, and then the very next
statement also grabs another section of heap memory, that second section of the heap might not
physically reside right after the first section you allocated.


Just as when scooping dirt from a big heap, one shovel does not pick up dirt granules that were right
below the last shovel of dirt. Also, when you throw the shovel of dirt back onto the heap, that dirt
doesn’t go right back where it was. Although this analogy might seem to stretch the concept of
computer memory, you’ll find that you’ll understand the heap much better if you think of the heap of
memory like you think of the heap of dirt: You have no idea exactly where the memory you allocate
and deallocate will come from or go back to. You know only that the memory comes and goes from
the heap.


If you allocate 10 bytes of heap memory at once, those 10 bytes will be contiguous. The important
thing to know is that the next section of heap memory you allocate will not necessarily follow the
first, so you shouldn’t count on anything like that.


Your operating system uses heap memory along with your program. If you work on a networked
computer or use a multitasking operating environment such as Windows, other tasks might be grabbing
heap memory along with your program. Therefore, another routine might have come between two of
your heap-allocation statements and allocated or deallocated memory.


You have to keep track of the memory you allocate. You do this with pointer variables. For instance,
if you want to allocate 20 integers on the heap, you use an integer pointer. If you want to allocate 400
floating-point values on the heap, you use a floating-point pointer. The pointer always points to the
first heap value of the section you just allocated. Therefore, a single pointer points to the start of the
section of heap you allocate. If you want to access the memory after the first value on the heap, you
can use pointer notation or array notation to get to the rest of the heap section you allocated. (See, the
last chapter’s pointer/array discussion really does come in handy!)


But Why Do I Need the Heap?


Okay, before learning exactly how you allocate and deallocate heap memory, you probably want more
rationalization about why you even need to worry about the heap. After all, the variables, pointers,
and arrays you’ve learned about so far have sufficed nicely for program data.


The heap memory does not always replace the variables and arrays you’ve been learning about. The
problem with the variables you’ve learned about so far is that you must know in advance exactly what
kind and how many variables you will want. Remember, you must define all variables before you use
them. If you define an array to hold 100 customer IDs, but the user has 101 customers to enter, your

Free download pdf