[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: realloc documentation

From: Pádraig Brady
Subject: Re: realloc documentation
Date: Tue, 21 Apr 2009 12:23:56 +0100
User-agent: Thunderbird (X11/20071008)

Bruno Haible wrote:
> Pádraig Brady wrote:
>> ... then we need to use realloc like this I think:
>>   errno=0
>>   np = realloc (p, n);
>>   if (np==NULL && errno==ENOMEM) {
>>     free (p);
>>     error ();
>>   }
>> I.E. xrealloc etc. should be changed to check ENOMEM as above?
> This way of checking realloc's return value is a correct one. But the one
> we have in lib/xmalloc.c currently is correct as well. The difference
> between the two is when p == NULL && n == 0 on glibc systems. In this case,
>   - your proposed code realizes that glibc was out of memory and fails,
>   - the current code returns NULL, like it would do on AIX and OSF/1 systems.
> Since xrealloc does not guarantee that xrealloc(NULL,0) != NULL, failing in
> this situation on glibc is unnecessary.

Right, that is subtle.
On glibc if realloc(NULL,0)==NULL happens then we've run out of memory.
But we don't care as our code shouldn't need to use any memory when size is 0.

So stuff for me todo:

1. Add a comment in xmalloc.c as to why we ignore
the above very unlikely out of memory condition.

2. Deprecate the realloc module as as it doesn't give
you anything and just confuses things.

3. It would be nice to document somewhere how one should
notice and handle errors in realloc(). Perhaps this should
be pushed out to the linux manpages which seem ambiguous,
and the glibc info docs which have an xrealloc() example
which looks incorrect.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]