Path: utzoo!attcan!uunet!salt.acc.com!ucsd!nprdc!stanonik From: stanonik@nprdc.navy.mil (Ron Stanonik) Newsgroups: u3b.misc,comp.unix.wizards Subject: unsigned sizeof() Message-ID: <6042@arctic.nprdc.arpa> Date: 14 Feb 90 16:27:18 GMT Sender: news@nprdc.arpa Reply-To: stanonik@nprdc.navy.mil (Ron Stanonik) Organization: Navy Personnel R&D Center, San Diego Lines: 27 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@nprdc.navy.mil ucsd!nprdc!stanonik