Turboc 2.0 malloc/free/coreleft
Kim Bloomquist
bloomqui at osiris.cso.uiuc.edu
Thu Jan 31 04:21:58 AEST 1991
I'm trying to write a Turbo C 2.0 program on a Zenith 386 that
reports the amount of memory (RAM) available after performing
certain operations. The following program produces the output
shown below:
---------------------------- tst.c --------------------------
#include <stdio.h>
#include <stdlib.h>
#include <alloc.h>
main()
{
unsigned long n;
char *s;
n = coreleft();
printf("ram left = %lu bytes free\n", n);
if ((s = (char *) malloc(80)) == NULL)
{
printf("allocation failed\n");
exit(0);
}
printf("buffer allocated\n");
n = coreleft();
printf("ram left = %lu bytes free\n", n);
/* printf("string: ");
gets(s); */
free((void *) s);
printf("buffer deallocated\n");
n = coreleft();
printf("ram left = %lu bytes free\n", n);
}
output from run 1: /* commented version */
ram left = 63704 bytes free
buffer allocated
ram left = 63616 bytes free
buffer deallocated
ram left = 63704 bytes free
The program performs as expected. Memory is allocated to hold s then
free() deallocates s making available the same number of bytes (63,704)
we started with. However, when the comments are removed from around the
printf/gets statements the following output is produced. The last call
to coreleft shows only 63,082 bytes free and not the original 63,690 bytes.
Question 1: What is being allocated to the missing 608 bytes?
Question 2: Can this memory (the 608 bytes) be deallocated?
output from run2: /* uncommented version */
ram left = 63690 bytes free
buffer allocated
ram left = 63602 bytes free
string:
buffer deallocated
ram left = 63082 bytes free
More information about the Comp.lang.c
mailing list