Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.2 9/18/84; site rtp47.UUCP
Path: utzoo!watmath!clyde!bonnie!akgua!mcnc!rti-sel!rtp47!throopw
From: throopw@rtp47.UUCP (Wayne Throop)
Newsgroups: net.lang.c
Subject: Re: Uses of "short" ?
Message-ID: <191@rtp47.UUCP>
Date: Sun, 15-Sep-85 15:06:20 EDT
Article-I.D.: rtp47.191
Posted: Sun Sep 15 15:06:20 1985
Date-Received: Tue, 17-Sep-85 04:52:34 EDT
References: <486@houxh.UUCP> <2761@sun.uucp> <697@cyb-eng.UUCP> <2789@sun.uucp> <710@cyb-eng.UUCP>
Organization: Data General, RTP, NC
Lines: 39

> > Even when writing grubby device driver code, you should use C as a
> > higher-level language.
> >         Guy Harris

> You are invited to write a 3Com Ethernet driver that works on a PC
> (16-bit int) and on a Cyb machine (32-bit int) without referring to longs
> or shorts.  I.e., a 16-bit hardware register is a 16-bit hardware register,
> despite your desire for abstraction.
>           Bill Crews

Well, it surely can't be done without refering to C's primitive types,
but I think that it should still be done abstractly.  You need a module
that "knows" about the physical-to-C-types mappings that you need to
use, and there you say

    typedef packet_id_type  short;    /* network needs 16 bits signed */
    typedef packet_len_type unsigned; /* network needs 32 bits unsigned */
    typedef io_register_type long;    /* a 32 bit signed register */
    ... etc, etc

and everywhere else in the code, you refer to the abstract types
packet_id_type, packet_len_type, io_register_type, and so on.  Thus, the
machine-dependant part of things can be kept to a very small part of the
world, by abstracting the machine (or specification) dependant types.

Why is this an advantage?  Because it allows you to have a "handle" on
things that are, say, io_register_type.  If you just declared them
"long" everywhere, you couldn't tell them from file positions, or
other things that might be declared "long".

In this sense, using "long" or "short" or "int" directly is often "too
low level" a use of C.  If you want "a small integer that won't get too
large" use "short".  But if you want "a thing that must be mapped to a
specific size and shape in bits", use an abstract type, not the
(changable) primitive type.  In "real" code for large systems, I'd
hope to almost *never* see *anything* declared to be any primitive
type.
-- 
Wayne Throop at Data General, RTP, NC
!mcnc!rti-sel!rtp47!throopw