"Numerical Recipes in C" is nonportable code

Chris Torek chris at mimsy.UUCP
Wed Sep 7 00:33:56 AEST 1988


[me, paraphrased by me:]
>An implementation may ABORT ON THE COMPUTATION of an illegal address.

In article <867 at osupyr.mast.ohio-state.edu> vkr at osupyr.mast.ohio-state.edu
(Vidhyanath K. Rao) asks:
>But why should it abort? If the address is sr:0, (sr = segment register)
>subtract 1 to get (sr-1):ffff [or whatever number of 'f's].

On many machines, addresses are unsigned numbers.  The domain and range
of an unsigned 16-bit number is 0..65535.  What is the (mathematical)
result of 0 - 1?  Answer: -1.  Is it in range?  No.  So what happens?
Integer underflow, which on many machines is a trap.

You can even do this on a VAX, although there you must first enable the
trap (use bispsw or set the appropriate flag in the subroutine entry
mask), and then it only fires on integer computations outside the range
-2 147 483 648..2 147 483 647; so if (for instance) you were to write

	main()
	{
		char *p;

		p = (char *)0x7fffffff;
		asm("bispsw	$0x20");	/* PSL_IV */
		p++;
	}

This program, when run, aborts with a `floating exception' (SIGFPE).
It would be legal for the C compiler to set IV in the entry point
of each subroutine, although it would probably break too much code
that expects integer overflow/underflow to be ignored, and the code
that does C's `unsigned' arithmetic would have to turn it off temporarily.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain:	chris at mimsy.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list