
144
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.
Based Pointers
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
convention.
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
memory.
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
. The
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

EXAMPLE: USING BASED POINTERS
145
keyword available with Microsoft C. An alternative is to map the file to an
array and use indexi ng to access records in the mapped files.
The program is written as yet another version of the
command, this time
called
. There are enough new features, however, to make it interesting.
•The records are of varying lengths.
• The program uses the first field as a key but detects its length.
•There are two file mappings. One mapping is for the original file, and the
other is for the file containing the sorted keys. The second file is the index file,
and
each of its records contains a key and a pointer (base add ress) in the
original file.
sorts the key file, much as in Program 5–4.
•The index file is saved and can be used later, a nd there is an option (
) that
bypasses the sort and uses an existing index file. The index file can also be
used to perform a fast key file search by performing a binary search (using,
perhaps, the C library
function) on the index file.
Figure 5–5 shows the relationship of the index file to the file to be sorted.
Program 5–5,
, is the main program that sets up the file mapping, sorts
the index file, and displays the results. It calls a function,
,
which is shown in Program 5–6.
Program 5–5
Based Pointers in an Index File

146
CHAPTER 5 MEMORY MANAGEMENT, MEMORY-MAPPED FILES, AND DLLS
Figure 5–5 Sorting with a Memory-Mapped Index File

EXAMPLE: USING BASED POINTERS
147
Program 5–6 is the function, which creates the index file.
It initially scans the input file to determine the key length from the first record.

148
CHAPTER 5 MEMORY MANAGEMENT, MEMORY-MAPPED FILES, AND DLLS
Subsequently, it must scan the input file to find the bound of each varying-length
record to set up the structure shown in Figure 5–5.
Program 5–6
Creating the Index File
Get Windows System Programming Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.