Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!uwm.edu!ux1.cso.uiuc.edu!osiris.cso.uiuc.edu!bloomqui From: bloomqui@osiris.cso.uiuc.edu (Kim Bloomquist) Newsgroups: comp.lang.c Subject: Turboc 2.0 malloc/free/coreleft Message-ID: <1991Jan30.172158.2769@ux1.cso.uiuc.edu> Date: 30 Jan 91 17:21:58 GMT Sender: news@ux1.cso.uiuc.edu (News) Organization: University of Illinois at Urbana Lines: 56 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 #include #include 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