Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!virtech!cpcahil From: cpcahil@virtech.uucp (Conor P. Cahill) Newsgroups: comp.lang.c Subject: Re: Malloc in Turbo C Keywords: Turbo C Malloc Message-ID: <1990Feb28.024306.8801@virtech.uucp> Date: 28 Feb 90 02:43:06 GMT References: <26316@qfagus.OZ> Reply-To: cpcahil@virtech.UUCP (Conor P. Cahill) Organization: Virtual Technologies Inc., Sterling VA Lines: 22 In article <26316@qfagus.OZ> gordon@qfagus.OZ (Peter Gordon) writes: >X head = (char **)malloc(200 * sizeof(char **)); >X for(cp = head, i = 0; i < 200; ++i, ++cp) >X { >X fprintf(stdout,"Freeing %d\n", i); >X fflush(stdout); >X free(cp); >X } The problem is that you can only call free with a pointer that is returned by malloc. In this code the only valid call to free is free(head) or free(cp) for the first loop. Due to a small amount of malloc chain info, most of your calls to free are ignored, but you hit just the right circumstances to cause a problem once in a while. -- +-----------------------------------------------------------------------+ | Conor P. Cahill uunet!virtech!cpcahil 703-430-9247 ! | Virtual Technologies Inc., P. O. Box 876, Sterling, VA 22170 | +-----------------------------------------------------------------------+