Turboc Malloc

Peter Gordon gordon at qfagus.OZ
Wed Feb 28 08:51:32 AEST 1990


>	Please tell me I'm doing something stupid.
>		head = (char **)malloc(200 * sizeof(char **));
>		for(cp = head, i = 0; i < 200; ++i, ++cp)
>		{
>			fprintf(stdout,"Freeing %d\n", i);
>			fflush(stdout);
>			free(cp);
>		}
>	}
My face is red, as suspected the fault is mine and is stupid.  I'm
freeing a pointer and THEN trying to increment it.
Code something like:
X	for(cp = &head[198]; cp >= head; --cp)
X		free(cp + 1);
X	free(head);
works as expected.  People abuse the bugs in compilers, but in many
instances, they are very forgiving of fools such as I.

Peter Gordon



More information about the Comp.lang.c mailing list