ksbszabo@wateng.UUCP (Kevin Szabo) (03/13/85)
In article <395@aicchi.UUCP> ignatz@aicchi.UUCP (Ihnat) writes: > char buff[64]; > setbuf(stdout,buff); > >Some boring poking with adb resulted in the not surprising >fact that the stack and data were crawling down each other's >throats; the only surprising thing was that this happened at all >on such a small, simple program. Has anyone else found this? Setbuf expects the buffer to be BUFSIZ big. STDIO has no way of finding out how big your buffer really is. Reading the man page for SETBUF should clarify this. Peeking at the source ain't bad either. Kevin -- Kevin Szabo watmath!wateng!ksbszabo (U of Waterloo VLSI Group, Waterloo Ont.)
wjafyfe@watmath.UUCP (Andy Fyfe) (03/14/85)
In article <2142@wateng.UUCP> ksbszabo@wateng.UUCP (Kevin Szabo) writes: >In article <395@aicchi.UUCP> ignatz@aicchi.UUCP (Ihnat) writes: >> char buff[64]; >> setbuf(stdout,buff); >> >>Some boring poking with adb resulted in the not surprising >>fact that the stack and data were crawling down each other's >>throats; the only surprising thing was that this happened at all >>on such a small, simple program. Has anyone else found this? > >Setbuf expects the buffer to be BUFSIZ big. STDIO has no >way of finding out how big your buffer really is. >Reading the man page for SETBUF should clarify this. >Peeking at the source ain't bad either. > > Kevin >-- >Kevin Szabo watmath!wateng!ksbszabo (U of Waterloo VLSI Group, Waterloo Ont.) Adding any one of the following lines to the end of your program will make the problem go away. exit(0); fclose(stdout); fflush(stdout); Once main ends, your buffer goes away (as it's local to main, and allocated on the stack), and anything in it may be lost. (It still gets the character count right!) If you make buff static, the problem also goes away. The buffer size will still be a problem, though, if you exceed 64 chars. --Andy Fyfe ...!{decvax, allegra, ihnp4, et. al}!watmath!wjafyfe wjafyfe@waterloo.csnet
ignatz@aicchi.UUCP (Ihnat) (03/14/85)
Well, I'm bit by the portability thing. For a long time, I used a copy of stdio that I'd rewritten about 3 years ago, to be cleaner & faster than the then-current version. In addition to some other things, it allowed variable-sized setbuf declarations. When I left the client, I had to leave that version, since there was some original source code in it. So, I'm now back to using vanilla System III stdio--and guess what. Yep, you're supposed to use the manifest buffer variable BUFSIZ. (Interestingly enough, however, the problem is caused not by stdio initializing the buffer, or anything like that; the stack is growing into the buffer on one of the returns from within csav3, while doing the cleanup at the end of the job. Moving the buffer to static data, or doing an fflush(), fixes the glitch; although it would break again on messages > the declared size) Hokay, so this is an object lesson in portability. -- Dave Ihnat Analysts International Corporation (312) 882-4673 ihnp4!aicchi!ignatz