Skip to main content
Logo image

Chapter 21 Computer Memory Management

Suppose you wanted to store prime numbers in an array so that you can quickly access them at a later time during the run of your program. Since there are infinitely many prime numbers it’s a bit unclear how large an array you should declare in your code. Suppose you ask the user at the start of execution how many primes they wish to work with. It would then make sense to, at that time, declare an appropriately sized array. Our techniques so far do not allow us to do so. With our ANSI C compiler, the size of an array has to be known at compile-time and cannot be given at runtime. We’ll now learn how to ask for memory at runtime.
In this chapter, we will learn how to dynamically allocate memory for variables. Just like how you could make a dinner reservation to ensure there is space in the restaurant for you, we can reserve space in memory for our variables.