Re: realloc() - frequency, conditions, or experiences about relocation?

Liste des GroupesRevenir à ss math 
Sujet : Re: realloc() - frequency, conditions, or experiences about relocation?
De : ben (at) *nospam* bsb.me.uk (Ben Bacarisse)
Groupes : comp.lang.c sci.stat.math
Suivi-à : comp.lang.c
Date : 19. Jun 2024, 23:24:35
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <87o77wsk18.fsf@bsb.me.uk>
References : 1 2 3 4 5 6 7 8 9 10
User-Agent : Gnus/5.13 (Gnus v5.13)
David Brown <david.brown@hesbynett.no> writes:

On 19/06/2024 17:36, Ben Bacarisse wrote:
Growing the buffer exponentially is simple and effective.
>
Yes, that's the general way to handle buffers when you don't know what size
they should be.
>
A better solutions for this sort of program is usually, as you say, asking
the OS for the file size (there is no standard library function for getting
the file size, but it's not hard to do for any realistic target OS).  And
then for big files, prefer mmap to reading the file into a buffer.
>
It's only really for unsized "files" such as piped input that you have no
way of getting the size, and then exponential growth is the way to go.
Personally, I'd start with a big size (perhaps 10 MB) that is bigger than
you are likely to need in practice, but small enough that it is negligible
on even vaguely modern computers. Then the realloc code is unlikely to be
used (but it can still be there for completeness).

There are other uses that have nothing to do with files.  I have a small
dynamic array library (just a couple of function) that I use for all
sorts of things.  I can read a file or parse tokens or input a line just
by adding characters.  Because of its rather general use, I don't start
with a large buffer (though the initial size can be set).

--
Ben.

Date Sujet#  Auteur
17 Jun 24 * Re: realloc() - frequency, conditions, or experiences about relocation?22Anton Shepelev
18 Jun 24 +- Re: realloc() - frequency, conditions, or experiences about relocation?1David Jones
19 Jun 24 +* Re: realloc() - frequency, conditions, or experiences about relocation?8David Duffy
19 Jun 24 i+* Re: realloc() - frequency, conditions, or experiences about relocation?6Malcolm McLean
19 Jun 24 ii+* Re: realloc() - frequency, conditions, or experiences about relocation?3Ben Bacarisse
19 Jun 24 iii`* Re: realloc() - frequency, conditions, or experiences about relocation?2David Brown
19 Jun 24 iii `- Re: realloc() - frequency, conditions, or experiences about relocation?1Ben Bacarisse
20 Jun 24 ii`* Re: realloc() - frequency, conditions, or experiences about relocation?2Anton Shepelev
8 Jul 24 ii `- Re: realloc() - frequency, conditions, or experiences about relocation?1Anton Shepelev
19 Jun 24 i`- Re: realloc() - frequency, conditions, or experiences about relocation?1Anton Shepelev
2 Jul 24 `* Re: realloc() - frequency, conditions, or experiences about relocation?12Rich Ulrich
2 Jul 24  +* Re: realloc() - frequency, conditions, or experiences about relocation?5Keith Thompson
2 Jul 24  i`* Re: realloc() - frequency, conditions, or experiences about relocation?4Rich Ulrich
8 Jul 24  i `* Re: realloc() - frequency, conditions, or experiences about relocation?3Anton Shepelev
22 Jul 24  i  `* Re: realloc() - frequency, conditions, or experiences about relocation?2Rich Ulrich
23 Jul 24  i   `- Re: realloc() - frequency, conditions, or experiences about relocation?1Anton Shepelev
2 Jul 24  `* Re: realloc() - frequency, conditions, or experiences about relocation?6Paul
2 Jul 24   `* Re: realloc() - frequency, conditions, or experiences about relocation?5Rich Ulrich
2 Jul 24    `* Re: realloc() - frequency, conditions, or experiences about relocation?4Rich Ulrich
2 Jul 24     `* Re: realloc() - frequency, conditions, or experiences about relocation?3Paul
2 Jul 24      +- Re: realloc() - frequency, conditions, or experiences about relocation?1James Kuyper
2 Jul 24      `- Re: realloc() - frequency, conditions, or experiences about relocation?1James Kuyper

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal