Use Huge Pointers if Data Item Is Larger than 64K

ID: Q41247


The information in this article applies to:


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: 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 
NOTE: The count parameter of many of these functions is of type size_t, which is an unsigned int (65535). Although these functions can accept huge pointers to objects in which the area being copied from and to can span a segment boundary, the function still only supports copying 64K worth of data at a time.

Additional query words: kbinf 1.00 1.50 6.00 6.00a 6.00ax 7.00 8.00 8.00c


Keywords          : kb16bitonly 
Version           : 
Platform          : 
Issue type        : 

Last Reviewed: July 22, 1999