randomness

Steve Summit scs at athena.mit.edu
Wed Apr 11 10:13:55 AEST 1990


In article <4878 at helios.TAMU.EDU> john at stat.tamu.edu (John S. Price) writes:
>srand(time(NULL));

Make that

	#include <sys/types.h>
	srand((int)time((time_t)NULL));

This is not mere pedantry; it does make a difference.  I'm
posting this reminder only because this code matches the one time
I ever got burned by a realio, trulio int/pointer mismatch, and
in case there is anyone out there who still isn't convinced that
proper pointer casts are in fact necessary in function call
arguments.  Somehow I forgot the cast one day and typed
srand(time(NULL)) into the planet's lowest quality/popularity
ratio computer (the PC) and compiled using a "memory model" in
which ints and pointers are of different size.  Since the
designers of that machine apparently believed that protection
mechanisms were only necessary in the antique mainframe computers
which the PC revolution repudiated, the result of this mistake
(on the PC) is not "Segmentation violation (core dumped)" but
rather a hung machine, which must be power-cycled and rebooted.

Watch those NULL pointers, folks.  (Yes; prototypes might have
helped.)
                                            Steve Summit
                                            scs at adam.mit.edu



More information about the Comp.lang.c mailing list