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

ID: Q107499

7.00 | 1.00 1.50 MS-DOS | WINDOWS kbprg kbprb

The information in this article applies to:

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:

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

Last Reviewed: July 23, 1997