Use Huge Pointers if Data Item Is Larger than 64K

Last reviewed: July 17, 1997
Article ID: Q41247
6.00 6.00a 6.00ax 7.00 | 6.00 6.00a | 1.00 1.50
MS-DOS                 | OS/2       | WINDOWS
kbprg

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

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

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 reference words: kbinf 1.00 1.50 6.00 6.00a 6.00ax 7.00 8.00
8.00c
KBCategory: kbprg
KBSubcategory: CLIss
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.