Skip to main content
Engineering LibreTexts

12.1: Introduction

  • Page ID
  • Up until now, whenever we have needed variables we simply declared them, either globally or locally. There are times, however, when this approach is not practical. Consider a program that must deal with a large amount of data from external files such as a word processor, or graphics or sound editor. In all instances the application may need to open very large files, sometimes many megabytes in size. The issue is not merely the size. After all, you could declare a very large array, or perhaps several of them. The problem is that the data is both large and variable in size. For example, you might edit a sound file that’s 100k bytes in size, but you might also need to edit one that’s 100 times larger. It would not be wise to declare a 10 megabyte array when you only need 100k. Further, you can guarantee that if you do declare 10 megabytes, the day will come when you’ll need 11 megabytes. What is needed is some way of dynamically allocating memory of the size needed, when needed.