rokicki@Navajo.ARPA (01/25/86)
*** REPLACE THIS LINE WITH YOUR BROTHER *** Here is a simple C routine I threw together which should determine the amount of memory accessible by C (at least a rough approximation.) When I run it, it dies; but it consists solely of calls to AllocMem! Any hints out there? /* * This routine finds out how much memory is available to a C program * by simply allocating successively larger pieces until it fails, * and then successively smaller pieces, adding them up as it goes. * This will only work if the memory is not terribly fragmented, but * if it is, it might not be that useful, eh? */ char *AllocMem() ; main () { char *p ; int i, j ; j = 0 ; for (i=4; 1; i<<=1) { p = (char *)AllocMem(i, 0) ; if (p == 0) break ; else { j += i ; printf("%d %d\n", i, j) ; } } for (; i>=4; i>>=1) { p = (char *)AllocMem(i, 0) ; if (p !=0) { j += i ; printf("%d %d\n", i, j) ; } } } It crashes the machine (at least it crashes my 512K machine). Help! I want my program to take advantage of the memory available, but if it can't even figure how much exists, how can it? -tom
neil@amiga.UUCP (Neil Katin) (01/30/86)
In article <301@Navajo.ARPA> rokicki@Navajo.ARPA writes: > Here is a simple C routine I threw together which should >determine the amount of memory accessible by C (at least a rough >approximation.) When I run it, it dies; but it consists solely of >calls to AllocMem! Any hints out there? Tom then goes on with a program that allocates all of memory. A much more reasonable way is to use the AvailMem() call. AvailMem(0) will return the total number of free bytes in the memory pool AvailMem( MEMF_LARGEST ) will return the largest contiguous chunk of memory (e.g. the largest AllocMem that will succeed ). Neil Katin Commodore-Amiga, Inc.