Re: OT: Language War (Re: "C" Manual)
Lo, on Tuesday, January 1, Ben Collins did write:
> On Mon, Dec 31, 2001 at 09:15:26PM -0600, Richard Cobbe wrote:
>
> > Consider the following:
> >
> > char *a, *b;
> >
> > a = strdup("This is a sample string");
> > b = a;
> >
> > free(a);
> >
> > /* Much code follows here, none of which modifies b. */
> >
> > printf("%s\n", b);
>
> Uh, for one, this wont segfault.
Perhaps, perhaps not. That's the thing about C---the behavior in this
case is completely undefined. Since printf is looking for a byte with a
value of zero, if it doesn't find such a byte before it walks off the
end of the process's allocated memory, it will segfault.
> Secondly, you can make this mistake with any language that allows
> references (perl, python, and java all allow it). Just replace free()
> with some other assignment that changes what a is, and ultimately you
> change b, which referenced it, unintentionally.
True. That, however, is not a type error of the sort that I'm
describing. And, in any case, the behavior of the program in that
situation is well-defined by the language specification. This is *not*
the case with C or C++.
Richard
Reply to: