Library Support for Near, Far, and Huge Keywords

ID: Q46820

6.00 6.00a 6.00ax 7.00 | 6.00 6.00a | 1.00 1.50

MS-DOS                 | OS/2       | WINDOWS
kbtool

The information in this article applies to:

   The Microsoft C/C++ Compiler (CL.EXE) included with:
    - Microsoft C for MS-DOS, versions 6.0, 6.0a, and 6.0ax
    - Microsoft C for OS/2, versions 6.0 and 6.0a
    - Microsoft C/C++ for MS-DOS, version 7.0
    - Microsoft Visual C++ for Windows, versions 1.0 and 1.5

SUMMARY

With Microsoft C 5.1, using the "near", "far", and "huge" keywords to override addressing conventions within specific memory models, you can usually use one of the standard libraries. Often, you cannot pass far pointers, or the addresses of far data items, to a small-model library routine. Some exceptions are the library routines "halloc", "hfree", and the "printf" family of functions.

With C versions 6.0 and later, there are far-pointer versions of all important functions, which can be used in mixed-model programming. The names of all these functions will begin with "_f". For example, the model- independent version of strcpy() will be called _fstrcpy().

MORE INFORMATION

Mixed-memory-model programming uses the "near", "far", and "huge" keywords to locate code or data outside of the segments specified by the memory model (for example, declaring a "far" character array in a small memory model where it would ordinarily be "near").

Mixed-model programming is useful when you have only a bit of data to put outside of the single data segment and you don't want to switch to a multiple-data-segment memory model. The standard library routines, however, were written to support the standard memory models, and so must be used with care.

The following example demonstrates a function that performs as expected in large or compact model but returns incorrect results in the medium or small model because its data, "buffer", was declared "far":

char far buffer[100];
. . . fwrite ( buffer, size, count, stream);

In single-data-segment models, data addresses are 2-bytes long. In multiple- data-segment models, data addresses are 4-bytes long. Data declared "far" also has 4-byte addresses. When fwrite() is called in a single-data-segment model, it expects 2-byte, not 4-byte, data addresses.

Removing the "far" keyword or compiling in a multiple-data-segment model corrects the problem. Another solution is to assign the value of the far variable to a near variable. In the example below, each element of the far array is assigned to the near array. A strcpy() cannot be used in this case, because it is one of the library functions that operates properly only when the data addresses are consistent with their memory model:

char far buffer[];
char nearbuffer;   /* near by default in small and medium models */ 
int i;

for (i = 0; i < 100; i++)
  nearbuffer[i] = buffer[i];
. . . fwrite ( nearbuffer, size, count, stream);

Additional reference words: kbinf 1.00 1.50 6.00 6.00a 6.00ax 7.00 8.00 8.00c mixed model KBCategory: kbtool KBSubcategory: CLIss Keywords : kb16bitonly

Last Reviewed: July 18, 1997