Sujet : Re: realloc() - frequency, conditions, or experiences about relocation?
De : richard (at) *nospam* damon-family.org (Richard Damon)
Groupes : comp.lang.cDate : 25. Jun 2024, 12:21:42
Autres entêtes
Organisation : i2pn2 (i2pn.org)
Message-ID : <v5e986$12a19$1@i2pn2.org>
References : 1 2 3 4 5 6 7 8
User-Agent : Mozilla Thunderbird
On 6/25/24 6:05 AM, Keith Thompson wrote:
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
On Mon, 24 Jun 2024 02:55:39 -0700, Keith Thompson wrote:
Lawrence D'Oliveiro <ldo@nz.invalid> writes:
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 ...
>
It's not safe to assume that a shrinking realloc call will never fail.
It's possible that it will never fail in any existing implementation,
but the standard makes no such guarantee.
>
...
>
Having said all that, if realloc fails (indicated by returning a null
pointer), you still have the original pointer to the object.
>
In other words, it’s safe to ignore any error from that last shrinking
realloc? That’s good enough for me. ;)
What? No, that's not what I said at all.
Suppose you do something like:
some_type *p = malloc(BIG_VALUE);
// ...
p = realloc(p, SMALL_VALUE);
If the realloc() succeeds and doesn't relocate and copy the object,
you're fine. If realloc() succeeds and *does* relocate the object, p
still points to memory that has now been deallocated, and you don't have
a pointer to the newly allocated memory. If realloc() fails, it returns
a null pointer, but the original memory is still valid -- but again, the
assignment clobbers your only pointer to it.
I presume you can write code that handles all three possibilities, but
you can't just ignore any errors.
The idiom I always learned for realloc was something like:
some_type *p = malloc(size);
if (!p) {
// allocation failed, do something about it. (might be just abort)
}
...
some_type *np = realloc(p, new_size);
if (np) {
p = np;
} else {
// p still points to old buffer, but you didn't get the new size
// so do what you can to handle the situation.
}
// p here points to the current buffer,
// might be the old size or the new.
Date | Sujet | # | | Auteur |
17 Jun 24 | realloc() - frequency, conditions, or experiences about relocation? | 95 | | Janis Papanagnou |
17 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Chris M. Thomasson |
17 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 53 | | Ben Bacarisse |
17 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 52 | | Malcolm McLean |
17 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 50 | | Ben Bacarisse |
17 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 49 | | Malcolm McLean |
17 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 21 | | Ben Bacarisse |
17 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 17 | | Anton Shepelev |
18 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 16 | | Tim Rentsch |
18 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 8 | | Malcolm McLean |
18 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 5 | | Malcolm McLean |
29 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 4 | | Lawrence D'Oliveiro |
2 Jul 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 3 | | Malcolm McLean |
2 Jul 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Ben Bacarisse |
4 Jul 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Lawrence D'Oliveiro |
24 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 2 | | Tim Rentsch |
24 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | David Brown |
20 Jun 24 | Indefinite pronouns [was:Re: realloc() - frequency, conditions, or experiences about relocation?] | 7 | | Anton Shepelev |
20 Jun 24 | Re: Indefinite pronouns | 3 | | vallor |
21 Jun 24 | Re: Indefinite pronouns | 2 | | David Brown |
21 Jun 24 | Re: Indefinite pronouns | 1 | | Keith Thompson |
20 Jun 24 | Re: Indefinite pronouns [was:Re: realloc() - frequency, conditions, or experiences about relocation?] | 2 | | Kenny McCormack |
20 Jun 24 | Re: Indefinite pronouns [was: Re: <something technical>] | 1 | | Janis Papanagnou |
21 Jun 24 | Re: Indefinite pronouns [was:Re: realloc() - frequency, conditions, or experiences about relocation?] | 1 | | Tim Rentsch |
17 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 2 | | Richard Harnden |
17 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Chris M. Thomasson |
17 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Malcolm McLean |
17 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 23 | | Anton Shepelev |
18 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | David Jones |
19 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 9 | | David Duffy |
19 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 7 | | Malcolm McLean |
19 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 4 | | Ben Bacarisse |
19 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 3 | | David Brown |
19 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 2 | | Ben Bacarisse |
20 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | David Brown |
20 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 2 | | Anton Shepelev |
8 Jul 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Anton Shepelev |
19 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Anton Shepelev |
2 Jul 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 12 | | Rich Ulrich |
2 Jul 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 5 | | Keith Thompson |
2 Jul 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 4 | | Rich Ulrich |
8 Jul 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 3 | | Anton Shepelev |
22 Jul 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 2 | | Rich Ulrich |
23 Jul 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Anton Shepelev |
2 Jul 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 6 | | Paul |
2 Jul 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 5 | | Rich Ulrich |
2 Jul 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 4 | | Rich Ulrich |
2 Jul 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 3 | | Paul |
2 Jul 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | James Kuyper |
2 Jul 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | James Kuyper |
17 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Chris M. Thomasson |
18 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 3 | | Keith Thompson |
18 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Malcolm McLean |
18 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Bonita Montero |
17 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | David Brown |
17 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 28 | | Bonita Montero |
20 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 27 | | Vir Campestris |
21 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 26 | | Bonita Montero |
24 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 25 | | Lawrence D'Oliveiro |
24 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 21 | | Keith Thompson |
24 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 11 | | David Brown |
24 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 7 | | Malcolm McLean |
24 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 3 | | Keith Thompson |
25 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Malcolm McLean |
25 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Chris M. Thomasson |
25 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 3 | | Lawrence D'Oliveiro |
25 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 2 | | Bonita Montero |
26 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Lawrence D'Oliveiro |
24 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 2 | | Chris M. Thomasson |
24 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Bonita Montero |
25 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Lawrence D'Oliveiro |
24 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Bonita Montero |
25 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 8 | | Lawrence D'Oliveiro |
25 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 6 | | Keith Thompson |
25 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Richard Damon |
28 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 2 | | Phil Carmody |
28 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Keith Thompson |
28 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 2 | | James Kuyper |
28 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Keith Thompson |
28 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | James Kuyper |
24 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 3 | | Bonita Montero |
24 Jun 24 | Down the hall, past the water cooler, third door on the left... (Was: realloc() - frequency, conditions, or experiences about) relocation? | 2 | | Kenny McCormack |
24 Jun 24 | Re: Down the hall, past the water cooler, third door on the left... (Was: realloc() - frequency, conditions, or experiences about) relocation? | 1 | | Bonita Montero |
17 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 2 | | David Brown |
17 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Bonita Montero |
17 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Janis Papanagnou |
17 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Michael S |
18 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Rosario19 |
25 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 7 | | Bonita Montero |
25 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 4 | | Vir Campestris |
25 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 3 | | Bonita Montero |
26 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 2 | | Vir Campestris |
26 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Bonita Montero |
25 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 2 | | DFS |
25 Jun 24 | Re: realloc() - frequency, conditions, or experiences about relocation? | 1 | | Bonita Montero |