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

Liste des GroupesRevenir à cl c  
Sujet : Re: realloc() - frequency, conditions, or experiences about relocation?
De : jameskuyper (at) *nospam* alumni.caltech.edu (James Kuyper)
Groupes : comp.lang.c
Date : 28. Jun 2024, 11:36:45
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v5m3nt$39uf0$1@dont-email.me>
References : 1 2 3 4 5 6 7
User-Agent : Mozilla Thunderbird
On 6/25/24 03:02, Lawrence D'Oliveiro wrote:
On Mon, 24 Jun 2024 02:55:39 -0700, Keith Thompson wrote:
...
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. ;)

No, you misunderstand:

    q = realloc(p, SMALL_VALUE);

Then if q is null, p still points at the originally allocated memory. If
q is not null, then it may point at newly allocated memory, and p has in
indeterminate value. You cannot go forward ignoring the possibility that
no new object was allocated, because if you do, you have no way of
knowing which of the two pointers you can safely dereference. You need,
at least,

    if(q)
        p = q;

then you can safely use p, regardless of whether realloc() allocated new
memory.

Date Sujet#  Auteur
28 Jun 24 o Re: realloc() - frequency, conditions, or experiences about relocation?1James Kuyper

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal