Placement of Data in the Compact, Large, and Huge Memory Models

In a memory model permitting multiple data segments, a global data item may be allocated in either the default data segment or in a far data segment. The data item's location and the way it is referenced depend on whether it is declared with a defining declaration or a referencing declaration (see Section 3.1 of the C Language Reference for more information).

Defining Declarations

Defining declarations include initialized data items and data items declared static, which are initialized to zero by default. The compiler can allocate space for data items in this category. These data items are placed in the default data segment unless their size exceeds a certain threshold. This threshold is specified by the /Gt option.

Option Effect

/Gt[[number]] Sets the threshold

The /Gt option causes all initialized data items whose size is greater than number bytes to be allocated to a new data segment. When number is specified, it must follow the /Gt option immediately, with no intervening spaces. When number is omitted, the default threshold value is 256. When the /Gt option is omitted, the default threshold value is 32,767.

This option is useful with programs that have more than 64K of initialized static and global data in small data items. Without this option, your program fills the default data segment and cannot be linked. The /Gt option does not apply to items declared with the __near or __far keywords.

Referencing Declarations

Referencing declarations include data items declared extern and uninitialized, nonstatic data items. The compiler cannot allocate space for data items in this category because it lacks information found in the other modules. When all the modules in the program are linked together, the linker can examine all references to these data items and determine where they are placed.

By default in the compact, large, and huge memory models, the compiler makes no assumptions about where the linker places those data items. All references to those data items are done with far addressing, in case they are placed in a far segment. If you can guarantee that your uninitialized and extern data items reside in the default data segment, you can use the /Gx option to have them referenced with near addressing. This improves the efficiency of your application.

Note:

If you reference a data item with near addressing, but declare it with __far in the module in which it is declared, your program will produce unpredictable results.

This option is also useful for writing compact-, large-, and huge-model Windows applications. If you want to run multiple instances of your Windows program simultaneously, you cannot use far addressing with your global data. If your global data resides in the program's default data segment, you can use the /Gx option to reference it as near. This allows you to run multiple instances of the program simultaneously.

Summary: Unsized arrays are treated as far.

The /Gx option affects how a data item is referenced only if the data's location is not otherwise specified. If an uninitialized or extern data item is declared with __near or __far, it is referenced as specified. If a data item is larger than the threshold specified by the /Gt switch, it is referenced with far addressing. Unsized arrays are treated as far because they might be larger than the threshold. You must explicitly declare an unsized array with __near if you want it referenced with near addressing.

The /Gx option does not affect pointers. Pointers remain far by default, and the dynamic allocation functions still return far pointers.