Dynamic vs. Static data

Chapter: Dynamic vs. Static data

Sometimes when you're programming, you know exactly how much memory any data your program encounters will need. For example, if you're writing a program to process data from a web page such as the Amazon shopping cart checkout page, you will find that the constraints imposed on the user's inputs are such that it's easy to select a fixed data structure (such as an array or a string) to receive the inputs. Any time you can specify definite sizes for your data, such as:

char phone_number[10];
char customer_first_name[20];
you are using static storage. Your program starts executing with exactly that storage allocated, and stays with that amount of storage throughout its execution. A feature of static storage is that each of your units of storage has a name, be it a variable name or an array cell reference like myarray[147].

On the other hand, in real life text areas should not limit the size of the user input. You don't know in advance, when processing, say the user information in the "Order Requirements" or "Additional Comments" fields, how large a structure you need to contain the user inputs.

Now if your program to handle the user inputs were to use static storage, there might come a time when a longwinded user types in more characters than your program has allocated space for. Consequences are not pretty:

A solution is to use dynamic data structures such as linked lists. Typically, it is possible to allocate more memory while a program is running. We lose the ability to refer to each unit of allocated storage by name. In C, the solution is to use pointers to the dynamic structures and store the pointer to the next structure somewhere in the current structure.


rhyspj@gwu.edu