Mail Archives: djgpp/1994/05/11/07:19:02
My previous postings on this problem were in a huge mess, hence no
replies. I do apologise. This is the problem:
I have been doing dynamic memory allocation using malloc,calloc and
free. Now with v1.11 I tried using
_go32_dpmi_remaining_physical_memory() to check what happens to my
memory. I compiled the program below with maint4, and ran it under
DOS and in a Windows DOS Box.
In Windows it indicated that no memory had been allocated, and in DOS
it kept on indicating the wrong values e.g. indicating that 16K has
been allocated for a 100byte array etc. (16K the page size?)
A similar 2-D allocation routine a la Dr. John Weiss has been used
and tested using this function. This shows the correct values being
allocated(using calloc) + small overhead, but it doesn't indicate the
data blocks being freed.
The simple example below has been tested using BorlandC++,
substituting farcoreleft() for
_go32_dpmi_remaining_physical_memory(). In this case it worked
perfectly.
Is the problem with my code,.._remaining_physical_memory() or with
the malloc() and free() commands not giving back the memory?
I notice a similar question was posted on memory in DV/X.
----------------------------------------------------------------------
#include <dpmi.h>
#include <std.h>
#include <malloc.h>
#include <math.h>
#include "nutils.h"
unsigned char *ucline;
int *iline;
int main()
{
unsigned long phys_mem,virt_mem,oldmem,newmem,allocmem,expected;
int width,n;
width=512;
phys_mem=_go32_dpmi_remaining_physical_memory();
virt_mem=_go32_dpmi_remaining_virtual_memory();
printf("start: physical memory = %ld , virtual memory =
%ld\n",phys_mem,virt_mem);
/*
// Test 1-D arrays
*/
printf("\nInput vector size: ");
scanf("%d",&width);
oldmem=phys_mem;
ucline=(unsigned char *)malloc(width*sizeof(unsigned char));
newmem=_go32_dpmi_remaining_physical_memory();
allocmem=oldmem-newmem;
expected=(unsigned long)(width*sizeof(unsigned char));
printf("UCvector(%d): allocated = %ld , expected = %ld ,diff =
%ld\n",width,allocmem,expected,allocmem-expected);
free(ucline);
newmem=_go32_dpmi_remaining_physical_memory();
printf("After freeing: memory now = %ld , expected = %ld ,
difference = %ld\n",newmem,oldmem,labs(newmem-oldmem));
oldmem=newmem;
iline=(int *)malloc(width*sizeof(int));
newmem=_go32_dpmi_remaining_physical_memory();
allocmem=oldmem-newmem;
expected=(unsigned long)(width*sizeof(int));
printf("Ivector(%d): allocated = %ld , expected = %ld ,diff =
%ld\n",width,allocmem,expected,allocmem-expected);
/* write to vector */
for(n=0;n<width;++n)
iline[n]=0;
free(iline);
newmem=_go32_dpmi_remaining_physical_memory();
printf("After freeing: memory now = %ld , expected = %ld ,
difference = %ld\n",newmem,oldmem,labs(newmem-oldmem));
getkey();
return(0);
}
----------------------------------------------------------------------
Nick van der Merwe
Department of Surveying and Geodetic Engineering
University of Cape Town |
South Africa | "Welcome to the Gnu South Africa"
Internet: neek AT cerecam DOT uct DOT ac DOT za
- Raw text -