Asdcarl.110 net.news utzoo!decvax!ucbvax!ucsfcgl!sdcarl!rusty Fri Dec 4 18:20:27 1981 Re: sdcarl.108: BUFLEN From sdcarl!ucsfcgl!ucbvax!decvax!duke!swd Fri Dec 4 10:09:07 1981 Subject: Re: sdcarl.108: BUFLEN ... Whether or not we succeded, our goals lead us to reject the use of malloc(3). There are several reasons. The first is speed: the sbrk system call can take a long time. Also, for small systems, being able to say 'size news' and know how large the program *really* is is important. I don't believe that sbrk() can take that long of a time. As for being able to get the true size I can agree with that. I suspect that the code required to guess at the proper size of buffer, and the when it finds that the line it just tried to read (and only got the begining of) won't fit in its buffer, allocate a bigger buffer (how big?), copy the old one and read some more, would be bigger than just having larger static buffers, and a lot slower. Also, what happens when malloc fails for lack of memory? It can be very messy to clean up and recover gracefully, particularly since your stack may choose that precise moment to need to be expanded (oops!). The code doesn't have to "guess at the proper size of the buffer" it can simply malloc a BUFLEN's worth. While reading the line (with getc probably) if it finds that it won't fit it doesn't allocate a bigger buffer and copy the old one (that would be stupid), it simply calls realloc (which might do a copy). How much bigger? How about BUFLEN/2 or BUFLEN/4? It doesn't make a big difference as long as the program doesn't try to allocate more memory than the system can give it and it doesn't want to reallocate in too small of chunks because then it could spend a lot of time in realloc. And if malloc or realloc fail for lack of memory it isn't "very messy to clean up and recover gracefully", it is quite easy. Nor should the code be that much bigger or slower. ----------------------------------------------------------------- gopher://quux.org/ conversion by John Goerzen of http://communication.ucsd.edu/A-News/ This Usenet Oldnews Archive article may be copied and distributed freely, provided: 1. There is no money collected for the text(s) of the articles. 2. The following notice remains appended to each copy: The Usenet Oldnews Archive: Compilation Copyright (C) 1981, 1996 Bruce Jones, Henry Spencer, David Wiseman.