Dynamic Memory Allocation
For the execution of the program, it is essential to bring the program into the main memory. When a program does not fit into the main memory, parts of it are brought into the main memory one by one, and program gets executed. Of course, the parts of the program that are not currently in the main memory are resident at the secondary memory, such as a floppy or a hard disk or any other magnetic disk. When a new segment of a program is to be moved into a full main memory, it should replace another segment that is already resident in the main memory. These are replacement policies, and their description is beyond the scope of this book. Hence, the programmer should not be concerned with the method of transfer of the program from secondary to primary memory or replacement policies.
The technique by which a program can obtain storage space in the main memory at run time is called dynamic allocation. In this method, the space for the program is allocated from the free space during the execution of the program. Memory requested by the program is allocated by the system from the memory heap. The free region of the memory is called the heap. The heap changes depending on the memory model used. Conceptually, the C++ programs are stored in the free space from the heap. Overall, 8086 memory models are described in this chapter. The amount of memory requirement is decided by how the program is designed. For example, if a program is developed with many recursive functions, then this is implemented with a stack.
The next section describes the C++ operators new and delete that can be used for achieving dynamic memory allocation and deallocation, respectively.