Under the compact, large, or huge memory model, the compiler allocates initialized data items as near if they are smaller than or equal in size to the threshold value set by the /Gt option. The /Gx option extends this initialized data allocation rule to data that is uninitialized and data that is marked as extern.
Without the /Gx option, the compiler makes no assumptions about where the linker places uninitialized or external data. All references to those data items are done with far addressing, in case they are placed in a far segment.
Near data offers two benefits:
The compiler can generate more efficient code to reference data it knows is near.
You can achieve multiple instances of a single Windows application when all data is near.
The /Gx option works only if the memory-model specification for each individual data declaration (and its definition) is consistent across compilation modules. That is, an individual data item is either __near everywhere, __far everywhere, or its memory model is not specified anywhere.
To ensure that all data is near, mark unsized arrays as __near, make sure that no data is marked as __far, and that the data-size threshold set with the /Gt option does not force anything far. For more information on the data-size threshold, see the /Gt option earlier in this chapter.
The /Gx option does not affect pointers. Pointers remain far by default, and the
dynamic allocation functions still return far pointers.
Use the /Gx option with either the /AC, the /AL, or the /AH option to modify the compact, large, or huge memory model, respectively. With /Gx, all three memory models still offer multiple code and data segments. For more information on memory models, see Chapter 4 of the Programming Techniques manual. For more information on referencing declarations, see Chapter 3 in the C Language Reference.
Examples
CL /AL /Gx ONE.C TWO.C
This example compiles and links two modules, ONE.C and TWO.C, using the large memory model. Assume that ONE.C contains an external declaration of data (for example, extern struct strr;), and TWO.C defines struct strr as __near. In this case, specifying /Gx allows the compiler to safely generate more efficient code than would be possible if the compiler had to assume that struct strr might be far.
CL /AL /Gx /Gt8 ONE.C TWO.C
This example compiles and links two modules, ONE.C and TWO.C, using the large memory model. Because the /Gt option sets a data threshold size of 8 bytes, the compiler assumes that all data items smaller than 8 bytes and either uninitialized or marked as extern are near.
Note:
If you reference a data item with near addressing but declare it with __far in another module, your program will produce unpredictable results.