Typedef names vs. new types.

Doug Philips dwp at k.gp.cs.cmu.edu
Thu Oct 20 13:37:38 AEST 1988


Regarding the recent discussion of typedef names and their intent and
use:  

I have found that it can be very useful to use typedef to create a
conceptual entity (this is probably an abuse of the mechanism).  The
problem with this (as mentioned before) is that the compiler doesn't
really consider this a new type.  Even lint is, at first glance, no
help.  However, you can somewhat easily get lint to help you out.  I'd
be interested in finding out what other solutions along these lines
people use.  My solution is as follows:

For convenience, I define all my new typedef names in one place.  The
rationale is that they often are used to indicate quantities and
ranges which are 'configurable' depending on what you want the range
of the type to be.  This is not a requirement of my solution however.

Consider a two dimensional graph where the new typedef'ed names are
arranged along the X axis.  The Y axis is the "size" of the type.
By plotting the points associated with each type/size pair (size is
determined by the derivee type), a "type terrain" is visible.  What
lint can do is tell you, in certain cases, where usage involves
elements that are at different altitudes.  Particularly useful to me
with a 4.2BSD lint is the ability to detect mixture of longs with
smaller types.  The initial problem is that it is the altitude of the
types that makes lint/cc unhappy with their intermixture.  What I do
is to walk the type terrain, and modify one typedef at a time.  If the
type is already a long, I make it an int and if it's not a long, I
make it one.  After each and every transformation I run lint to catch
the cases where I'm assigning with mixed types.  When I move onto the
next type in the terrain I restore the previous type to its original
value.

The obvious next step is to come up with some way of automating the
process so that I'll do it more frequently.

		-Doug

-- 
Doug Philips                  "All that is gold does not glitter,
Carnegie-Mellon University       Not all those who wander are lost..."
dwp at cs.cmu.edu                     -J.R.R. Tolkien



More information about the Comp.std.c mailing list