Re: [hackers] [st] Add xmalloc and xrealloc wrappers || "Roberto E. Vargas Caballero"

From: Joerg Zinke <>
Date: Thu, 13 Sep 2012 23:40:04 +0200

Am 13.09.2012 um 20:18 schrieb Andrew Hills <>:

> On Thu, Sep 13, 2012 at 10:37 AM, Joerg Zinke <> wrote:
>> This xrealloc() is a good a example for bad c programming.
>> Quoting the OpenBSD realloc(3) manpage:
> (...)
>>> +void *
>>> +xrealloc(void *p, size_t len) {
>>> + if((p = realloc(p, len)) == NULL)
>>> + die("Out of memory");
>>> + return p;
>>> +}
> I don't see how those guidelines apply to xrealloc(). If realloc()
> ever fails, the program exits, so there is no memory leak.

The programming language does not define what happens before
and after program execution. Modern OS may cleanup your memory
on exit() older ones may not.

> If you
> meant the code calling xrealloc() (formerly calling realloc()), then
> it's not xrealloc() that's an example of bad practice.

Always checking bounds before each (x)realloc call is better than doing it
at on place?

> But, "row" and
> "col" are already checked for the lower bound; the size of the
> structures is known and small, and it is a reasonable assumption that
> no one will have a screen resolution high enough to overflow these, or
> even come close, so I see no reason to check the upper bound.

The name xrealloc() suggests generic realloc() replacement not a
'works for row and col specific case' realloc().
Received on Thu Sep 13 2012 - 23:40:04 CEST

This archive was generated by hypermail 2.3.0 : Thu Sep 13 2012 - 23:48:05 CEST