Microsoft KB Archive/107499

From BetaArchive Wiki

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


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 #include 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 #include #include 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
Issue type :
Technology : kbVCsearch kbAudDeveloper kbvc150 kbvc100

Last Reviewed: May 6, 2001
© 2001 Microsoft Corporation. All rights reserved. Terms of Use.