Out-of-bounds pointers

Alan J Rosenthal flaps at dgp.toronto.edu
Sun Oct 8 03:14:05 AEST 1989


scott at bbxsda.UUCP (Scott Amspoker) writes:
>What was once bad coding style now was considered a bug.  Take the following
>code fragment as an example:
>
>my_proc()
>   {
>   register char *p;
>
>   p = (char*)malloc(1000);
>   free(p);  /* free never returns but core dumps instead - why? */
>   }

No, no, no.  It was never said that this code fragment wasn't conforming.  It
is, other than the missing declaration of malloc() which I assume you assumed
was present previously in the file.  free() has to work correctly.  The user
cannot dereference invalid pointers, but the compiler can if it knows that it
is safe to do so, and must not dereference invalid pointers if it doesn't know
whether or not it is safe.

And this fragment was never considered bad coding style, again apart from the
fact that you are mallocing memory and not doing anything with it.  You seem to
be claiming that use of free() is now non-portable and was always considered
bad coding style.

ajr



More information about the Comp.std.c mailing list