Dynamic allocation

From Wikipedia, the free encyclopedia

Dynamic allocation, often called heap memory, is one of the types of memory allocation that happens inside of a computer program. A program can be divided into two logical parts: text and data. Text is the actual machine code that is executed by the computer. The data is the information on which the code operates as it executes.

The data, in turn, can be divided into a number of logical parts according to how it is stored and used. Two different types used in C programs are: stack and dynamic memory. Dynamic memory is allocated during program execution and allows the program to be flexible in how it allocates and uses memory. The amount of memory used by a program will be in proportion to the size of the collection of data it is working with—a program working with a large collection of data dynamically creates a large work space; a program operating on a smaller collection creates a smaller work space and uses as little computer storage or RAM as necessary. In other words the dynamic memory devices need to be charged to keep the data stored on them. When the charging becomes less the data becomes unavailable.

Consider this. A computer programmer writes a program that reads some data into RAM, works on it, and displays results. He would like to handle data with arbitrary size from 10 bytes to 10 megabytes (10 million bytes) or more. However, all of the data must be in RAM at one time to do the work. The programmer doesn't want to have to allocate 10 megabytes of space when they may only be reading in a 10 byte file because it is wasteful of system resources. The programmer is also worried that the program may have to handle data larger than 10 megabytes. Dynamic memory allows the program to handle small, large, and huge amounts of data efficiently by only using the necessary resources to complete the job.

In other languages