Mail Archives: djgpp/2001/12/12/19:17:39
Hello.
Waldemar Schultz wrote:
>
> CBFalconer schrieb:
> > > dest = realloc (dest, (strlen (dest) + strlen (arg) + 1) * sizeof
> > > (char));
> > ^^^^ ^^^^
> > automatically creates a memory leak on any realloc failure. You
> > want:
> >
> > if (NULL == (temp = realloc(dest, /* whatever */))) {
> > /* failure recovery */
> > }
> > else dest = temp;
>
> Right. So shouldn't we modify the info libc realloc
The docs have already been fixed in DJGPP CVS. When DJGPP 2.04 is
released, the realloc page will go a little something like this:
realloc
=======
Syntax
------
#include <stdlib.h>
void *realloc(void *ptr, size_t size);
Description
-----------
This function changes the size of the region pointed to by PTR. If it
can, it will reuse the same memory space, but it may have to allocate a
new memory space to satisfy the request. In either case, it will
return the pointer that you should use to refer to the (possibly new)
memory area. The pointer passed may be `NULL', in which case this
function acts just like `malloc' (*note malloc::).
An application that wants to be robust in the face of a possible failure
of `realloc' to enlarge a buffer should save a copy of the old pointer
in a local variable, to be able to use the original buffer in case
`realloc' returns `NULL'. See the example below for details.
Return Value
------------
On success, a pointer is returned to the memory you should now refer
to. On failure, `NULL' is returned and the memory pointed to by PTR
prior to the call is not freed.
Portability
-----------
ANSI, POSIX
Example
-------
if (now+new > max)
{
char *old = p;
max = now+new;
p = realloc(p, max);
if (p == NULL)
p = old; /* retain the old pointer */
}
Bye,
--
Richard Dawe
http://www.phekda.freeserve.co.uk/richdawe/
- Raw text -