INF: Use Huge Pointers if Data Item Is Larger than 64K

ID Number: Q41247

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

MS-DOS | OS/2

Summary:

There are three types of data pointers in Microsoft C: near, far, and

huge. Near pointers represent offsets within DGROUP (the default data

segment) and are stored in 2 bytes. Far and huge pointers contain both

a segment address/selector and an offset, and therefore take 4 bytes.

However, the arithmetic done on far and huge pointers is different.

Therefore, huge pointers should be used for an item that will cross a

segment boundary.

More Information:

Although far and huge pointers are identical in format, the algorithms

used to do addressing calculations involving these pointers are very

different. Far pointers are assumed to point to a data item that does

not cross a segment boundary (in other words, the size of the item

must be less than 64K). As a result, the compiler ignores the segment

part of the pointer in all calculations except for "equals" and "not

equals" tests. This gives a considerable savings in execution time

(more than twice as fast) for these operations. In fact, calculations

involving far pointers are almost as fast as calculations involving

near pointers.

Huge pointers may point to items that are larger than 64K. The

addressing arithmetic works on both the segment and the offset. Huge

pointer arithmetic is therefore considerably slower than far

arithmetic, but it has the advantage of working when the data item is

larger than 64K.

When compiling with the huge memory model (/AH), all pointers are huge

pointers. If there are only a few items that need huge pointers, it

would be better to use a large memory model and explicitly declare

those items huge; otherwise, there is a considerable performance hit.

Use one of the following methods to declare an array of 75,000 chars:

1. Declare a huge pointer and allocate memory with halloc:

char _huge *array;

array = (char _huge *) halloc( 75000L, sizeof( char ) );

2. Explicitly declare a huge array:

char _huge *array[75000L]

Note that a capital L is placed at the end of an integral constant to

indicate that it should be interpreted as a long int rather than an

int.

Huge pointer arithmetic is not supported in the small and medium model

run-time libraries. In the compact and large model libraries, only the

following functions support huge pointer arithmetic:

bsearch _fmemchr _fmemmove lfind

fread _fmemcmp _fmemset lsearch

fwrite _fmemcpy halloc memccpy

_fmemccpy _fmemicmp hfree memchr

Additional reference words: 5.00 5.10 6.00 6.00a 6.00ax 7.00