Pointer Problems

When you declare items to be __near, __far, __huge, or __based, you can link with a standard run-time library. Be aware, however, that in some cases, the modified pointers will be incompatible with standard library functions. Watch for these problems that affect pointers:

A library function that expects a 16-bit pointer as an argument will not function properly with modified variables that occupy 32 bits. In other words, you can cast a near pointer to a far pointer, because it adds the segment value and maintains the integrity of the address. If you cast a far pointer to near, however, the compiler generates a warning message because the offset may not lie within the default data segment, in which case the original far address is irretrievably lost.

A library function that returns a pointer will return a pointer of the default size for the memory model. This is only a problem if you are assigning the return value to a pointer of a smaller size. For example, there may be difficulties if you compile with a model that selects far data pointers, but you have explicitly declared the variable to receive the return value __near.

This warning does not apply to all functions. Microsoft C/C++ provides model-independent versions of its string and memory functions such as _fstrcat, the far version of strcat.

Based pointers pose a special problem. Based pointers are passed to other functions as is (without normalization). Certain functions expect to receive based pointers, but most do not. Therefore, in most cases, you must either explicitly cast a based pointer to a far pointer or make sure that all functions that receive based pointers are prototyped.

Some run-time library functions support near, far, huge, and based variables. For example, _halloc allocates memory for a huge data array.

You can always pass the value (but not the address) of a far item to a small-model library routine. For example,

/* Compile in small model */

#include <stdio.h>

long __far time_val;

void main()

{

time( &time_val ); /* Illegal far address */

printf( “%ld\n”, time_val ); /* Legal value */

}

When you use a mixed memory model, you should include function prototypes with argument-type lists to ensure that all pointer arguments are passed to functions correctly.