[net.lang.c] Uses of Short

cottrell@nbs-vms.ARPA (COTTRELL, JAMES) (09/09/85)

/*
Someone asked `what are the uses of short?' with specific interest in
machines where `sizeof(short) = sizeof(int)' (he really means `==' :-)
to which Guy Harris replies: `AAAAAAIIIIEEEEEEEEEE' followed by lots of
words of wisdom. Except he didn't answer the question: `What *are* the
uses of short?' I don't disagree with anything he said & I would sincerely
like to hear his answer. `What are the stimuli that would provoke Guy
to respond by typing `short' at the C compiler?'

Perhaps the type short was conceived as a way to conveniently write
machine *dependent* code, (such as getting at device registers) &
create explicitly packed struxures for the nonportable parts of the
operating system. If one is supplying the source code, then even such
construx as the following will be portable with sufficient warning.

	#ifdef PARANOID
	typedef	short	small;
	#else
	typedef	int	small;

OOPS! I got it backwards (I'm typing directly, no editor), make that
`#ifndef'. The ANSI C std should help clear up matters as well.

	jim		cottrell@nbs
*/
------

guy@sun.uucp (Guy Harris) (09/11/85)

> `What are the stimuli that would provoke Guy to respond by typing `short'
> at the C compiler?'

How about an array of 10000 counters, each one of which won't ever get over
10000.  Perfect opportunity for "short" on any machine, especially machines
like mine which has 32-bit "int"s.

"short" is also useful for declaring members of a structure which is
defined by some agency other than the coder (like the hardware designer, in
the case of a device register, or like another program).  Then again, if the
data structure is to be exported to other machines, the agency in question
may want to define it using some language other than C, since you have to
specify byte order, padding between/alignment of members, etc., etc..

	Guy Harris

preece@ccvaxa.UUCP (09/13/85)

> > `What are the stimuli that would provoke Guy to respond by typing `short'
> > at the C compiler?'
>  How about an array of 10000 counters, each one of which won't ever get
> over 10000.  Perfect opportunity for "short" on any machine, especially
> machines like mine which has 32-bit "int"s.  /* Written  7:40 pm  Sep
> 10, 1985 by guy@sun.uucp in ccvaxa:net.lang.c */
----------
Of course, on some machines halfword arithmetic is slower than fullword
arithmetic and on others the opposite is true, so if speed is a major
consideration you may need to tailor your declaration to the specific
machine you're working on.  Maybe "int" should by convention be
the whatever style integer is the fastest to work with...

-- 
scott preece
gould/csd - urbana
ihnp4!uiucdcs!ccvaxa!preece