_vmalloc() Allocation Limited to 64K

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

The information in this article applies to:

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

SUMMARY

The _vmalloc() function fails when attempting to allocate a memory block greater than 64K minus 6 bytes. Even though the type of the block size parameter passed to _vmalloc() is an unsigned long, the maximum block that can be allocated with a single call to _vmalloc() is slightly less than 64K.

MORE INFORMATION

The _vmalloc() function will return _VM_NULL if the block size requested is 65531 bytes or larger. The Virtual Memory Manager (VMM) is only able to associate a single 64K block of memory with a virtual memory handle. The extra 6 bytes are used to store internal information about the memory block.

The block size limitation is due to the way the Virtual Memory Manager uses conventional memory to swap in virtual memory blocks. When an application allocates a block of virtual memory, the memory must be brought down to a swap area in conventional memory. This is similar to the page frame implemented by expanded memory. The swap area in conventional memory that the Virtual Memory Manager uses is limited to 64K bytes.

The sample code below fails on the call to _vmalloc() because the size requested exceeds the 64K minus 6 byte limit.

Sample Code

/ * Compile options needed: none

 */

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

void main( void )
{
    _vmhnd_t handle;
    unsigned long block_size;
    unsigned long mem_size;

    if ( !_vheapinit( 0, _VM_ALLDOS, _VM_XMS | _VM_EMS ) )
    {
        printf( "Could not initialize virtual memory manager.\n" );
        exit( -1 );
    }

    /* The following allocation request will fail. */

    if ( (handle = _vmalloc( 65531L )) == _VM_NULL )
    {
        _vheapterm();
        printf("Allocation failed!");
        exit( -1 );
    }
    _vfree( handle );
    _vheapterm();
}


Additional reference words: kbinf 7.00 1.00 1.50 _vmalloc _vrealloc
KBCategory: kbprg kbcode
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.