Re: realloc(): invalid next size



Michael Wojcik wrote:
The result of realloc should always be stored in a temporary
variable and should be checked for null. If it is null, remember
to free the old value:

char *newkey;
newkey = realloc(key, p);
if (! newkey)
{
free(key);
[perform error handling]
}
key = newkey;

Uh? Doesn't realloc(), when it doesn't fail, call free() all by itself
if there's a need for that? Also suppose the reallocated memory doesn't
'move' (newkey == key); doing a "free(key);" now could be disastrous.

--
If you're posting through Google read <http://cfaj.freeshell.org/google>
.



Relevant Pages

  • Re: realloc(): invalid next size
    ... newkey = realloc; ... Also suppose the reallocated memory doesn't ... The code above only frees key if realloc failed. ... If the reallocation fails and NULL is assigned to newkey, ...
    (comp.lang.c)
  • Re: realloc(): invalid next size
    ... char *newkey; ... newkey = realloc; ... Doesn't realloc(), when it doesn't fail, call freeall by itself ...
    (comp.lang.c)
  • Re: realloc(): invalid next size
    ... char *newkey; ... newkey = realloc; ... The code above only frees key if realloc failed. ...
    (comp.lang.c)
  • Re: realloc(): invalid next size
    ... Pedro Graca schrieb: ... char *newkey; ... newkey = realloc; ... only taken after a failure in realloc. ...
    (comp.lang.c)