BUG: _vmalloc() May Fail on Block Allocations

ID: Q127201

1.50 1.51 1.52 MS-DOS kbprg kbbuglist

The information in this article applies to:

SYMPTOMS

When using virtual memory allocation function _vmalloc() in the C runtime library, the program may fail on _vmalloc() if all of the following conditions are met :

The program allocates and frees the virtual memory blocks correctly for the first iteration of the outer For loop, but fails on the second iteration of the outer For loop when the inner For loop goes to iteration 512.

RESOLUTION

To work around the problem, use one of these two suggestions:

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. We are researching this problem and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

Sample Code to Reproduce Problem

/* Compile options needed: None
*/ 

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

#define NUM_ELEMENT 1000
#define SIZE        2042

_vmhnd_t vm_handle[NUM_ELEMENT];

int main()
{
   int i;
   unsigned long n;
   unsigned hmem = NUM_ELEMENT;
   char __far *pntr;

   printf("\nTest allocating virtual memory");

   // Initializing virtual memory managers.
   if(_vheapinit(0, _VM_ALLDOS, _VM_ALLSWAP)==0)
   {
     printf("\nError initializing virtual memory manager\n");
     exit(-1);
   }

   printf("\nVirtual memory manager initialization done");

   // Allocating virtual memory.
   for ( i=0; i<2; i++ )
   {
     printf("\n\nAllocating memory... ");
     for( n=0; n<hmem; n++ )
     {
       vm_handle[n] = _vmalloc(SIZE);
       if ( vm_handle[n]==_VM_NULL )
       {
         printf("Cannot allocate virtual memory. Entry = %d\n", n);
         _vheapterm();
         exit(-1);
       }
     }
     printf("Done\n");

     // Testing the virtual memory allocation.
     printf("Testing... ");
     for ( n=0; n<hmem; n++ )
     {
       pntr = (char __far *)_vload(vm_handle[n], _VM_DIRTY);
       if( pntr==NULL )
       {
         printf("Error in testing %d", n);
         exit(-1);
       }
     }
     printf("Done\n");

    // Deleting virtual memory allocated earlier.
    for ( n=0; n<hmem; n++ )
      _vfree(vm_handle[n]);

    printf("Memory freed...Done\n");
  }

  _vheapterm();

  return(1);
}

REFERENCES

For more information on virtual memory block allocation and page allocation, please see the following article in the Microsoft Knowledge Base:

ARTICLE-ID: Q93211

TITLE     : INF: _vmalloc() May Allocate Larger Memory Block Than Expected

Additional reference words: 1.50 1.51 buglist8.00 KBCategory: kbprg kbbuglist KBSubCategory: VirtualMem Keywords : kb16bitonly

Last Reviewed: July 23, 1997