ID: Q127201
1.50 1.51 1.52 MS-DOS kbprg kbbuglist
The information in this article applies to:
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 :
_vheapinit(...);
// _vmalloc() fails on the second iteration when j=512.
for ( i=0; i<2; i++ )
{
for ( j=0; j<1000; j++ )
handle[j] = _vmalloc(...);
for ( j=0; j<1000; j++ )
_vfree( handle[j] );
}
_vheapterm();
To work around the problem, use one of these two suggestions:
-or-
for ( i=0; i<2; i++ ) {
_vheapinit(...);
for ( n=0; n<1000; n++ )
handle[n] = _vmalloc(...);
for ( n=0; n<1000; n++ )
_vfree( handle[n] );
_vheapterm();
}
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.
/* 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);
}
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