Path: ...!weretis.net!feeder8.news.weretis.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!raubtier-asyl.eternal-september.org!.POSTED!not-for-mail From: Bonita Montero Newsgroups: comp.lang.c Subject: Re: realloc() - frequency, conditions, or experiences about relocation? Date: Mon, 24 Jun 2024 16:16:07 +0200 Organization: A noiseless patient Spider Lines: 18 Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Mon, 24 Jun 2024 16:16:07 +0200 (CEST) Injection-Info: raubtier-asyl.eternal-september.org; posting-host="25e5eda89458a83976f4ab5b7ad682c9"; logging-data="1030772"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19F6+Pp4/qLdNBTYD6cT4nU1oZfo7oGVkM=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:PHfZoQZ7MtSXM8DpMEiqWMfdaf8= Content-Language: de-DE In-Reply-To: Bytes: 2222 Am 24.06.2024 um 10:40 schrieb Lawrence D'Oliveiro: > On Fri, 21 Jun 2024 21:12:12 +0200, Bonita Montero wrote: >> Usually you don't resize the block with a few bytes ... > The usual way I use realloc is to maintain separate counts of the number > of array elements I have allocated, and the number I am actually using. A > realloc call is only needed when the latter hits the former. Every time I > call realloc, I will extend by some minimum number of array elements (e.g. > 128), roughly comparable to the sort of array size I typically end up > with. > And then when the structure is complete, I do a final realloc call to > shrink it down so the size is actually that used. Is it safe to assume > such a call will never fail? Hmm ... In C++ you dont't have to think about that. Do an emplace_back and the capacity() doubles with libstdc++ and libc++ if the vevtor becomes full.