Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxn!ihnp4!qantel!lll-lcc!lll-crg!seismo!rochester!mayer From: mayer@rochester.ARPA Newsgroups: net.lang.c++ Subject: snextc function in class streambuf Message-ID: <17685@rochester.ARPA> Date: Mon, 28-Apr-86 16:42:26 EDT Article-I.D.: rocheste.17685 Posted: Mon Apr 28 16:42:26 1986 Date-Received: Fri, 2-May-86 22:52:41 EDT Sender: mayer@rochester.ARPA Organization: U of Rochester, CS Dept., Rochester, NY Lines: 50 From: mayer The function "streambuf::snextc()" is undefined unless the stream buffer has been filled at least once. In particular, the sequence: filebuf input(0); int c = input.snextc(); results in a segmentation fault. The problem could be fixed quite easily by changing the implementation from: inline int snextc() { return ((++gptr)==pptr) ? underflow() : *gptr & 0377; } to inline int snextc() { return ((++gptr)>=pptr) ? underflow() : *gptr & 0377; } This wouldn't be any less efficient on most machines, and would allow define "snextc" appropriately over the buffer's entire life. By the way, replacing "*gptr & 0377" with "*(unsigned char *)gptr" will save a little space and time on most machines. For example, the SUN (V2) compiler produces: movb a5@,d0 andl #255,d0 for the first implementation, and moveq #0,d0 movb a5@,d0 for the second. This saves two 16 bit words of extention and 800ns on each call. Perhaps the buffers should be "unsigned char" also. I don't usually pick bits like this, but these routines are at the heart of the IO system. -- Jim -- Jim Mayer University of Rochester (arpa) mayer@Rochester.ARPA Department of Computer Science (uucp) rochester!mayer Ray P. Hylan Building (via allegra, decvax, or seismo) Rochester, New York 14627