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

(Romina) #1

26. Maximizing Your Computer’s Memory


In This Chapter


  • Thinking of the heap

  • Understanding why you need the heap

  • Allocating the heap

  • Taking action if there’s not enough heap memory

  • Freeing heap memory

  • Handling multiple allocations


Absolute beginners to C aren’t the only ones who might find this chapter’s concepts confusing at first.
Even advanced C programmers get mixed up when dealing with the heap. The heap is the collection
of unused memory in your computer. The memory left over—after your program, your program’s
variables, and your operating system’s workspace—comprises your computer’s available heap
space, as Figure 26.1 shows.


FIGURE 26.1 The heap is unused memory.

Many times you’ll want access to the heap, because your program will need more memory than you
initially defined in variables and arrays. This chapter gives you some insight into why and how you
want to use heap memory instead of variables.


You don’t assign variable names to heap memory. The only way to access data stored in heap memory
is through pointer variables. Aren’t you glad you learned about pointers already? Without pointers,
you couldn’t learn about the heap.


Note

The free heap memory is called free heap or unallocated heap memory. The part of the
heap in use by your program at any one time is called the allocated heap. Your program
might use varying amounts of heap space as the program executes. So far, no program
Free download pdf