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

Liste des GroupesRevenir à ss math 
Sujet : Re: realloc() - frequency, conditions, or experiences about relocation?
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.c sci.stat.math
Date : 19. Jun 2024, 19:41:49
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v4v58t$230rh$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0
On 19/06/2024 17:36, Ben Bacarisse wrote:
Malcolm McLean <malcolm.arthur.mclean@gmail.com> writes:
 
No. We have to have some knowledge. And what we probaby know is that the
input is a file stored on someone's personal computer.  And someone has
published on the statistical distribution of such files
 That's not the case that matters (to me at least).  If the input is a
file, we have a much better way of "guessing" the size than guessing and
growing -- just ask for the size.  Sure, we might need to make
adjustments if the file is changing, but there is always a better
measure than any statistical analysis.
 To some extent this seems like a solution in search of a problem.
It seems more like a solution that doesn't exist in search of a problem with absurdly unrealistic requirements.  And even if Malcolm's solution existed, and the problem existed, it /still/ wouldn't work - knowing the distribution of file sizes tells us nothing about the size of any given file.

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).

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