unsigned sizeof()

Ron Stanonik stanonik at nprdc.navy.mil
Thu Feb 15 03:27:18 AEST 1990


We ran into a "feature" while porting a program to our 3B2/600G
running sysV rel 3.2.2 V3: sizeof() returns unsigned.

The code looked like

	struct arf buf[MAX-1];
	while ((cnt = read(fd, buf, sizeof(buf))) >= sizeof(buf[0])) {
		do stuff with cnt arfs in buf
	}

The intent, apparently, is to reduce the number of reads by reading
several arf structs at once.  The problem occurs when the read returns
-1; since sizeof is unsigned the -1 is coerced into a big positive
number.

Yes, 2nd edition K&R says the result of sizeof is unsigned (though
it also says the type is implementation-defined, p204, which I find
contradictory).  Why though?  Why make sizeof unsigned?

Sorry if this is old news.  It was a surprise to us; ie, sizeof
on our suns and vax (4bsd) returns signed.

Thanks,

Ron Stanonik
stanonik at nprdc.navy.mil
ucsd!nprdc!stanonik



More information about the Comp.unix.wizards mailing list