INF: Library Support for Near, Far, and Huge Keywords

ID Number: Q46820

5.00 5.10 6.00 6.00a 6.00ax 7.00 | 5.00 5.10 6.00 6.00a 6.00ax

MS-DOS | OS/2

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.

For additional information, refer to the "Microsoft C Optimizing

Compiler User's Guide," for version 5.1, page 145.

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 instance, 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: 5.00 5.10 6.00 6.00a 6.00ax 7.00 mixed

memory model