CHAPTER 5 MEMORY MANAGEMENT, MEMORY-MAPPED FILES, AND DLLS
File mapping has two other notable limitations.
• A file mapping cannot be expanded. You need to know the maximum size
when creating the file mapping, and it may be difficult or impossible to
determine this size.
•There is no way to allocate memory within a mapped
memory region without
creating your own memory management functions. It would be convenient if
there were a way to specify a file mapping and a pointer returned by
and obtain a heap handle.
Summary: File Mapping
Here is the standard sequence required by file mapping.
1. Open the file. Be certain that it has
2. If the file is new, set its length either with
below) or by using
followed by .
3. Map the file with
4. Create one or more views with
5. Access the file through memory references. If necessary, change the mapped
6. On completion, perform, in order,
, for the
mapping handle, and
for the file handle.
Example: Sequential File Processing with Mapped Files
The program (Program 2–4) illustrates sequential file processing by convert-
ing ASCII files to Unicode, doubling the file length. This is an ideal application for
memory-mapped files because the most natural way to convert the data is to
process it one character at a time without being concerned with file I/O. Progr
5–3 simply maps the input file and the output file—first computing the output file
length by doubling the input file length—and converts the characters one at a
This example clearly illustrates the trade-off between the file mapping com-
plexity required to initialize the program and the resulting processing s
This complexity may not seem worthwhile given the simplicity of a simple file I/O
implementation, but there is a significant performance advantage. Appendix C
shows that the memory-mapped version can be considerably faster than the file