Signage on Acorn?

Richard A. O'Keefe ok at atlas.otago.ac.nz
Thu Apr 27 03:02:59 UTC 2000


Jay wrote:
	You are only allowed to use "char" as a type if you don't care about the
	arithmetic behavior.  This is true when you're really operating on
	*characters*, and it's the right thing for efficiency, because the compiler
	for each platform can choose the best representation.

Well, no.  I teach a C course from time to time, and I drill my students
with this question and answer:

    Q.  What is the one numeric type in C that *can't* be safely used
        to hold characters?
    A.  char

The use of char means that things like

	void copy_translated(char *dst, char *src, char *map) {
	    char c;
	    
	    while (c = *src++) *dst++ = map[c];
	    *dst = '\0';
	}

can result in undefined behaviour.  How?  If src points to
<G,a,u,ss,NUL> where the ss character is Latin 1 16rDF, and if
char is signed, then the 4th iteration of the loop will use a
negative index into map.  Whatever it does, it won't access the
right element.  I've found this mistake a lot in student code,
and have a surprisingly hard time making the students believe
it _is_ a mistake or that the problem is real.  The macros in
<ctype.h> act like array references in this respect.

Treat _every_ instance of char or char* with suspicion, and don't
generate any.





More information about the Squeak-dev mailing list