__far and Global Variables

The __far keyword can also be used 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 perform the same addressing mode for this 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 if you mix __far declarations or definitions within a non-__FAR_SEG data segment. Assuming that __FAR_SEG segments have extended addressing mode and normal segments have direct addressing mode, the following listing and the following listing clarify this behavior:

Listing: OK - 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 MyDirectSet

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.