CHAPTER 5 MEMORY MANAGEMENT, MEMORY-MAPPED FILES, AND DLLS
memory, and write it. Such a solution, included on the book’s Web site, would be as
effectiv e as Program 5 –4 and is often faster, as shown in Appendix C.
File maps are convenient, as the preceding examples demonstrate. Suppose, how-
ever, that the program creates a data structure with pointers in a mapped file and
expects to access tha t file in the future. Pointers will all be relative to the virtual
address returned from
, and they will be meaningless when
mapping the file the next time. The solution is to use based pointers, which are
actually offsets relative to another pointer. The Microsoft C syntax, available in
Visual C++ and some other systems, is:
Here are two examples.
Notice that the syntax forces use of the , a practice that is contrary to Windows
Example: Using Based Pointers
Previous programs have shown how to sort files in various situations. The object,
of course, is to illustrate different ways to manage memory, not to discuss sorting
techniques. Program 5–1 uses a binary search tree that is destroyed after each
sort, and Program 5–4 sorts an array of fixed-size records in mapped
Appendix C shows performance results for different implementations, including
the next one in Program 5–5.
Suppose that it i s necessary to maintain a permanent index file representing
the sorted keys of the original file. The apparent solution is to map a file that
contains the permanent ind e
x in a search tree or sorted key form to memory. Un-
fortunately, there is a ma jor difficulty with this solution. All pointers in the tree,
as stored in the file, are relative to the address returned by
next time the program runs and maps the fi le, the pointers will be useless.
Program 5–5, together with Program 5–6, solves this problem, which is char-
acteristic of any mapped data structure that uses pointers. The solution uses the