__far and Global Variables

You can also use the __far keyword for global variables:

  int __far i;        // OK for global variables  
  int __far *i;       // OK for global variables  
  int __far *__far i; // OK for global variables  

This forces the Compiler to address the variable as if it has been declared in a __FAR_SEG segment. Note that for the above variable declarations or definitions, the variables are in the DEFAULT_DATA segment if no other data segment is active. Be careful when mixing __far declarations or definitions within a non- __FAR_SEG data segment. Assuming that __FAR_SEG segments use extended addressing mode and normal segments use direct addressing mode, the following two examples ( the listings Acceptable, Consistent Declarations and Mixing Extended Addressing and Direct Addressing Modes) clarify the resulting behavior:

Listing: Acceptable, Consistent Declarations
#pragma DATA_SEG MyDirectSeg        // use direct addressing mode
int i;       // direct, segment MyDirectSeg

int j;       // direct, segment MyDirectSeg

#pragma DATA_SEG __FAR_SEG MyFarSeg /* use extended addressing mode */

int k;       // extended, segment MyFarSeg

int l;       // extended, segment MyFarSeg

int __far m; // extended, segment MyFarSeg
Listing: Mixing Extended Addressing and Direct Addressing Modes
// caution: not consistent!!!!
#pragma DATA_SEG MyDirectSeg  /* use direct-addressing mode */

int i;       // direct, segment MyDirectSeg

int j;       // direct, segment MyDirectSeg

int __far k; // extended, segment MyDirectSeg

int __far l; // extended, segment MyDirectSeg

int __far m; // extended, segment MyDirectSeg
Note: The __far keyword global variables only affect the variable addressing mode and NOT the allocation.