Article View: comp.lang.c.moderated
Article #2529Re: void* type
From: Stan Ryckman
Date: Sat, 06 Apr 1996 14:52
Date: Sat, 06 Apr 1996 14:52
57 lines
3079 bytes
3079 bytes
In article <4jttgp$3nh@solutions.solon.com>, telnet user <tim@wagner.Princeton.EDU> wrote: >msb@sq.com wrote: [I wrote] :: > > The realloc can certainly do something like use sz+10000 :: > > to "grow" the tmp area in tunable chunks, depending on the application. :: > :: > Better yet: unless memory is really tight, have it double the size. :: > This will set a fairly reasonable upper bound on the number of reallocs :: > you will have to do. : :: Better yet: change "unless memory is really tight" to "if memory is :: plentiful". The doubling method can run out of memory when the amount :: available is almost twice the amount that you need, not even considering :: the possibility of fragmentation. :Of course, if temporary realloc'ed buffers are a significant :percentage of a program's memory usage, it's design should be :rethought anyway ... (Q to moderator: are mentions of misuse of the English language disallowed? I'm getting tired of "it's" and "its" being used interchangeably.) Not necessarily. Besides, as proposed here, the buffer is not temporary. The first version used temporary malloc'd buffers whenever sz exceeded some MAX_EXPECTED_SIZE. :Under normal circumstances, O(log N) reallocs beats O(N) reallocs any :day (where N is the largest size buffer needed). Huh? Neither adding a fixed number to sz, nor using 2*sz, has anything to do with O(anything) here. Both do a fairly trivial number of realloc()'s and then reach the needed size, compared to the original method, which would have required malloc() and free() on every call to the function when MAX_EXPECTED_SIZE was poorly guessed. As Mark pointed out, doubling leaves you with the potential of using, say, 90% of the memory when you only need 46%. I hereby modify my original suggestion, however, that realloc() be used. It's probably better (or equal) to free() and then malloc() the new buffer, since at the time of the resizing, there is no need to preserve its contents. (I worked hard to get a sentence using both "it's" and "its"!! :) :--------------------------------------------------------------------------- :Tim Hollebeek | Disclaimer :=> Everything above is a true statement, :Electron Psychologist | for sufficiently false values of true. :Princeton University | email: tim@wfn-shop.princeton.edu :----------------------| http://wfn-shop.princeton.edu/~tim (NEW! IMPROVED!) Oh, you talk to electrons? That explains it! :) Cheers, Stan. -- Stan Ryckman (stanr@tiac.net)
Technical Details
Message-ID:
<4k60ff$ae7@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<4iepre$ag7@solutions.solon.com> <4jol6e$1rr@solutions.solon.com> <4jq1o1$a8v@solutions.solon.com> <4jttgp$3nh@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: stanr@tiac.net (Stan Ryckman) Newsgroups: comp.lang.c.moderated Subject: Re: void* type Date: 6 Apr 1996 08:52:31 -0600 Organization: Amber & Sneakers Fan Club Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4k60ff$ae7@solutions.solon.com> References: <4iepre$ag7@solutions.solon.com> <4jol6e$1rr@solutions.solon.com> <4jq1o1$a8v@solutions.solon.com> <4jttgp$3nh@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com