delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/12/12/19:17:39

X-Authentication-Warning: delorie.com: mailnull set sender to djgpp-bounces using -f
From: Richard Dawe <rich AT phekda DOT freeserve DOT co DOT uk>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: gcc bug or am I blind?
Date: Wed, 12 Dec 2001 23:22:29 +0000
Lines: 76
Message-ID: <3C17E6B5.72EC6670@phekda.freeserve.co.uk>
References: <3C17418A DOT 85364005 AT ma DOT tum DOT de> <3C174718 DOT EF323B32 AT ma DOT tum DOT de> <3c175119 AT news DOT starhub DOT net DOT sg> <3C17652A DOT C898D48F AT yahoo DOT com> <3C178A6F DOT 8B8DF9A0 AT ma DOT tum DOT de>
NNTP-Posting-Host: modem-180.beryllium.dialup.pol.co.uk
Mime-Version: 1.0
X-Trace: newsg2.svr.pol.co.uk 1008202381 30587 62.136.3.180 (13 Dec 2001 00:13:01 GMT)
NNTP-Posting-Date: 13 Dec 2001 00:13:01 GMT
X-Complaints-To: abuse AT theplanet DOT net
X-Mailer: Mozilla 4.77 [en] (X11; U; Linux 2.2.19 i586)
X-Accept-Language: de,fr
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019