Path: utzoo!attcan!uunet!hodgenet!georgemartin From: georgemartin@hodgenet.UUCP (George H. Martin) Newsgroups: comp.sys.ibm.pc Subject: Re: Freeing allociated memory. Message-ID: <223@hodgenet.UUCP> Date: 12 Jan 90 03:25:20 GMT References: <1990Jan10.202458.17223@resrch.molienergy.bc.ca> Reply-To: georgemartin@hodgenet.UUCP (George H. Martin) Distribution: na Organization: Homer Dodge Network Systems, Inc. Lines: 50 In article <1990Jan10.202458.17223@resrch.molienergy.bc.ca> jamesd@resrch.molienergy.bc.ca (James Dudley) writes: > >I am experiencing a problem with freeing memory allociated which was >allociated with malloc(). Consider the following code. > >char *buf, *lineptr[2000]; > >if (buf = (char *) malloc(fsize*sizeof(char)) == NULL) { > print error message and return >} >lineptr[0] = buf; > do stuff >/* now free allociated memory */ >free((void *) buf); > >Free() doesnot appear to deallociate the memory because I am using a >routine to traverse the Heap and report back Free and Unused blocks. > >Using MSC 5.1 compiled with Small memory model. >Any ideas? > In the UNIX/'C' environment: The free(3c) libc function call NEVER returns the calling program's data space to the operating system. Once space is allocated through sbrk(2) system calls during a program's call to malloc, it is NEVER returned. The free(3c) function removes the memory to be freed from the programs virtual memory, but retains the actual data space for possible future reallocation (in whole or in part) by subsequent calls to malloc. To maintain control of your data space, you must utilize the sbrk(2) and brk(2) system calls to actively, dynamically manipulate it. These calls allow you to mainpulate the break value of the program; the end of the data space (data segment in DOS terms). Of course, this assumes the MSC 5.1 environment supports these calls. Turbo C does. To what extent the free(3c) call performs in a 'C'DOS environment as it does in a UNIX environment, I do not know. NOTE: At least in UNIX. you never should malloc for space and then do a brk(2) or sbrk(2) to free it. Obviously, the tables maintained by malloc for its housekeeping will not be updated if you do this. The next time you call malloc your program will likely crash due to a "corrupt arena". Just some background. Hope it helps. -- George H. Martin uunet!hodgenet!georgemartin Homer Dodge Network Systems, Inc. +1 201 454 3333