When we speak about a program having one code or data segment or multiple code or data segments, we're referring to ”memory models.“ Microsoft C 6 supports five memory models that you can use for Windows programs:
Model | Code Segments | Data Segments |
Small | 1 | 1 |
Medium | Multiple | 1 |
Compact | 1 | Multiple |
Large | Multiple | Multiple |
Huge | Multiple | Multiple |
Command-line switches of the compiler determine the memory model you use. The small memory model is the default, but programs that have more than 64 KB of code must contain two or more code segments, and programs that have more than 64 KB of data must contain two or more data segments. In medium-model and large-model programs, all functions (unless explicitly declared as near) are far, and the compiler generates far calls for them. In compact-model and large-model programs, all data references use far pointers.
The small, compact, medium, and large models all have their own C libraries. The medium-model and large-model libraries contain functions that assume they have been called from another segment. The functions in the compact-model and large-model libraries always assume they have been passed long pointers to data. The huge model is essentially the same as the large model, except that individual data items may be greater than 64 KB. The huge model has limited library support.
Most small Windows programs are compiled as small-model programs, with one code segment and one data segment. Strictly speaking, however, Windows programs are really ”mixed-model“ programs, because they extensively use the near and far keywords. Windows programs make far calls to Windows functions, and Windows makes far calls to functions within a program, such as window procedures or call-back functions. All data pointers passed between a program and Windows (with one oddball exception—the GetInstanceData function) are far pointers.
Windows programs can also be compiled as medium-model programs. You can try this out on any of the programs shown so far. You need to make two changes to the make file:
Add the -AM switch to the compile step. This compiles for the medium model.
Change SLIBCEW to MLIBCEW in the link step. This is the library that contains the Windows-specific C run time library functions.
Delete the .OBJ file and run NMAKE. You'll find that the .EXE file is somewhat larger than before because all the functions within your program—not only the window procedure and call-back functions—now require far calls.