Chapter 5. Memory Management, Memory-Mapped Files, and DLLs
Most programs require some form of dynamic memory management. This need arises whenever it is necessary to create data structures whose size cannot be determined statically when the program is built. Search trees, symbol tables, and linked lists are common examples of dynamic data structures.Windows provides flexible mechanisms for managing a program's dynamic memory. Windows also provides memory-mapped files to associate a process's address space directly with a file, allowing the OS to manage all data movement between the file and memory so that the programmer never needs to deal with ReadFile, WriteFile, SetFilePointer, or the other file I/O functions. With memory-mapped files, the program can maintain dynamic data structures conveniently in permanent files, and memory-based algorithms can process file data. What is more, memory mapping can significantly speed up sequential file processing, and it provides a mechanism for memory sharing between processes.Dynamic link libraries (DLLs) are an essential special case of file mapping and shared memory in which files (primarily read-only code files) are mapped into the process address space for execution.This chapter describes the Windows memory management and file mapping functions, illustrates their use with several examples, and describes both implicitly and explicitly linked DLLs.