Path: utzoo!attcan!uunet!tank!ncar!mailrus!cornell!batcomputer!gdykes From: gdykes@batcomputer.tn.cornell.edu (Gene Dykes) Newsgroups: comp.windows.x Subject: Xtk "unsigned int" typedefs considered harmful Message-ID: <6289@batcomputer.tn.cornell.edu> Date: 12 Sep 88 18:54:12 GMT Distribution: comp Organization: Theory Center, Cornell U., Ithaca NY Lines: 43 The Facts --------- "Dimension", "Position", and "Cardinal" are typedef'd as unsigned ints in lib/Xt/Intrinsics.h. The Problem ----------- The following code fragment is a very reasonable thing to find in a geometry manager widget: Dimension new_width, old_width, adjustment ; Cardinal number_of_children ; ... new_width = old_width + adjustment / number_of_children ; I think that it's reasonable to think of an "adjustment" to a Dimension as being of type Dimension, too. But if adjustment is, say, -100, then this code clearly won't work. But, for the sake of argument, let's say that adjustment should definitely be an "int". Then, the code above still croaks out, and I think that this violates the principle of least surprise. One needs a cast as follows: new_width = old_width + adjustment / (int) number_of_children ; I got tired of trying to find every place that needed a cast in my widgets, so I threw up my hands and put the following in my header file: #define Dimension int #define Position int #define Cardinal int There are probably other circumstances like this, and I contend that there will soon be lots of these bugs lurking around, just waiting for the unexpected negative number to come along and blow the program away. Isn't it worth limiting Dimensions to two billion or so, by specifying them as "int" instead of "unsigned int"? I would presume that "Cardinal", etc., are typedef'd because the programmer is not supposed to need to know what they really are, when in fact that ignorance leads to big trouble. (My apologies if this has been hashed out before...) -- Gene Dykes, gdykes@tcgould.tn.cornell.edu