Malloc in Turbo C

Conor P. Cahill cpcahil at virtech.uucp
Wed Feb 28 13:43:06 AEST 1990


In article <26316 at qfagus.OZ> gordon at 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     |
+-----------------------------------------------------------------------+



More information about the Comp.lang.c mailing list