Re: Buffer or Realloc?
- From: Flash Gordon <spam@xxxxxxxxxxxxxxxxxx>
- Date: Mon, 28 Aug 2006 19:49:45 +0100
websnarf@xxxxxxxxx wrote:
bwaichu@xxxxxxxxx wrote:Is it generally better to set-up a buffer (fixed sized array) and read
and write to that
buffer even if it is larger than what is being written to it? Or is it
better to allocate memory and realloc it for the size of the what is
being written each time? In other words, what is the decision factor
between deciding to use a fixed size buffer or allocating memory
space and reallocing the size?
Fixed size arrays are good for things that whose contents *CANNOT*
exceed the size of that array. A string usually is never that kind of
thing.
So usually the decision is quite simple: if you are dealing with
mutable strings *NEVER* use a fixed size array (this is a definitive
mark of an amateur and is usually accompanied by either buffer overflow
errors or ruthless truncation),
OK, so for the string I've got to prepare as part of a message to the UK Government gateway where the specification says the string has a maximum length of 10 characters I should not use a fixed size buffer but a reallocating buffer? Presumable so that I don't immediately allocate the maximum size I should start at 1 byte and increase the size 1 byte at a time so avoid wasting memory?
You should not say to *NEVER* do something as invariably someone can come up with a *real* example where that rule does not apply, such as the one I just mentioned. I've got plenty of other interfaces where I am either preparing a string (and yes, I do want a C string for simplicity of manipulation such as using sprintf to create it) which has either a *fixed* final length or a small but definite upper bound.
> but instead use the reallocing buffer
strategy. There is a common tendency in C programmers to conflate char
arrays and strings -- the two are different things conceptually and
this has implications for what you are actually doing.
True. Life would be much easier if C had a true string type that automatically resized, but it doesn't, and when you know a reasonable and definite upper bound on size a fixed length array is perfectly adequate to the job.
If you just need a block buffer or something like that, or you are
transforming your algorithm to run through chunks of a stream at a
time, then obviously you can just use a fixed sized buffer.
True.
<snip>
malloc, calloc and realloc are fairly slow function calls. So one
thing you should generally do is try to minimize the number of times
you call such functions. When reusing a buffer like you are doing
above, you only want to realloc more space if you need it.
In addition you can hit memory fragmentation issues.
--
Flash Gordon
See, I don't always disagree with everything you say.
.
- Follow-Ups:
- Re: Buffer or Realloc?
- From: Michael Mair
- Re: Buffer or Realloc?
- References:
- Buffer or Realloc?
- From: bwaichu@xxxxxxxxx
- Re: Buffer or Realloc?
- From: websnarf
- Buffer or Realloc?
- Prev by Date: Re: int main()?
- Next by Date: Re: C IDE Recommendations
- Previous by thread: Re: Buffer or Realloc?
- Next by thread: Re: Buffer or Realloc?
- Index(es):
Relevant Pages
|