Abstraction

Wayne Throop throopw at rtp47.UUCP
Sat Sep 28 04:36:51 AEST 1985


In the "short vs int vs long" discussion, a topic comes up that I'd like
to comment on.  To review:

> >>  But I can't quite fiqure out what you mean here.  Does it mean that
> >>  is if 'short' 'int' and 'long' are the same size then I should
> >>  choose something other than 'int'?
>
> >   You did figure out what I meant - that is exactly what I mean!  If
> >   you want to have a variable that can hold values outside the range
> >   -32767 to 32767, use "long", regardless of whether something else
> >   just might happen to work.
>
> If one is very concerned about portability to other-wordlength machines, why
> not use typdefs like:
>
> 		typedef int bit16;
> 		typedef long bit32;
>
> Then make all your declarations as bit16, bit32, etc.
>
>               Robert Clark

Good idea.  But beware of using these when what you mean is not what
the typedef implies.  In particular, many implementers typedef physical
layouts as above when what they really want to abstract is *not*
physical layout, but something else again.

For example, on a Unix(tm) system, should one declare a variable to hold
a process id as a bit16, and is that more "abstract" than defining it
"short"?  I maintain that "proper" use of C would typedef (or #define)
an abstract handle for "pid_type", and then declare variables that hold
pids to be the abstract type.

Thus, "bit16", "bit32", and the like are good ideas, but beware.  These
names should *only* be used where physical layout is the major concern
(that is, almost nowhere).
-- 
Wayne Throop at Data General, RTP, NC
<the-known-world>!mcnc!rti-sel!rtp47!throopw



More information about the Comp.lang.c mailing list