PRB: R6013 "Illegal Far-Pointer Use" When Using Vmalloc

Last reviewed: July 22, 1997
Article ID: Q107499
7.00 | 1.00 1.50 MS-DOS | WINDOWS kbprg kbprb

The information in this article applies to:

  • Microsoft C/C++ compiler for MS-DOS, version 7.0
  • Microsoft Visual C++ for Windows, versions 1.0 and 1.5

SYMPTOMS

Accessing memory allocated with vmalloc() causes the error:

   run-time error R6013
   -illegal far-pointer use

CAUSE

This error is a result of using the /Zr compile option with the Virtual Memory functions. When you enable pointer checking, the compiler generates code to check all pointers to see whether they are outside the bounds of the program's address space. These values are stored in the global variables _aseglo and _aseghi. The global variable _aseglo is set at run- time to the lowest data segment value; _aseghi is set to the highest program segment.

When you initialize virtual memory with _vheapinit(), far memory is allocated outside of your program's address space. Therefore, it fails the test for greater than _aseghi, hence the error message.

RESOLUTION

There are two options:

  • Disable pointer checking. This option is primarily used during debugging and will incur a performance penalty on your code if you release with it still on.

    -or-

  • Reset _aseghi before you access the pointer. For example,

          #include <vmemory.h>
          #include <dos.h>
    

          extern unsigned int _aseghi;
    

          void main(void)
          {
    
            _vheapinit(...);
            _vmalloc(...);
            _vload(...);
            _aseghi = FP_SEG(ptr);
            //...
          }
    
    
  • The problem with this last option is that the primary benefit of using pointer checking is now obscured because the value of _aseghi has been modified.

Sample Code

/* Compile Options needed: /Zr
*/

#include <stdio.h>
#include <stdlib.h>
#include <vmemory.h>

struct {
 double x;
 } __far *buffer;

void main( void )
{
   _vmhnd_t handle;

   if ( !_vheapinit( 0, _VM_ALLDOS, _VM_XMS | _VM_EMS ) )
   {
      printf( "Could not initialize virtual memory manager. \n" );
      exit( -1 );
   }
   if ( ( (handle = _vmalloc( 10000 * sizeof(int) )) == _VM_NULL ))
   {
      _vheapterm();
      exit( -1 );
   }

   if ( ( buffer = _vload ( handle, _VM_CLEAN )) == NULL )
   {
      _vheapterm();
      exit( -1 );
   }

   buffer->x;            // R6013 occurs here

   _vfree( handle );
   _vheapterm();
   exit( 0 );
}


Additional reference words: 7.00 1.00 1.50
KBCategory: kbprg kbprb
KBSubcategory: VirtualMem
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 22, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.