Chapter 8
Heap Memory
8.1 Creating Array with malloc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
8.2 The Stack and the Heap . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
8.3 Functions that Return a Heap Address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
8.4 Two-Dimensional Arrays in C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
8.5 Pointers and Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
Chapter 2 describes one type of memory: stack memory (also called the call stack). Stack
memory follows a simple rule: first in, last out. The call stack is automatically managed by
the code generated by the compiler. Every time a function is called, a new frame is pushed.
Every time a function ends, the frame is popped. Programmers have no direct control
over this process. One consequence is that a function may read from or write to memory
addresses in lower frames; however, a function can never “look upward” into memory above
its frame. This is because whenever a function is actively executing, there are not valid
memory addresses “above” it.
This is natural and convenient for a lot of programming tasks; however, sometimes
programmers need more control over memory. They want to be able to allocate memory
when necessary and release memory when the allocated memory is no longer needed. For
this purpose, computers have another type of memory: heap memory.
Computers also have the third type of memory where the compiled code resides. In
general, this memory cannot be modified. Programs can access only stack memory and
heap memory. Thus, the rest of this book does not explain the memory where programs are
stored.
8.1 Creating Array with malloc
If a called function can access the frame of the calling (lower) function through pointers,
would this be sufficient? No. At the bottom of the call stack is the frame of the main
function. Can a program store all of its data in the main function, since this data can be
accessed by all the other functions? This would make the main function rather complex for
even a simple program. We would need to consider every possible way the data could be
read or written and the main would have to manage space for all of the data. Even if we
are willing to do this analysis and write this main function, we still have problems. Most
programs take some form of input. For example, we may need to read an image file from a
disk. At the time the code is being written, we cannot know precisely what input will be
given to the program. In particular, we do not know the size of the input. We cannot write
a program that can handle the inputs whose size is greater than our expectation. This is
a severe limitation. We could create an array in main and make its size greater than all
113