tobold.org

correct • elegant • free

△ Comp.lang.c △

How to write an EOF in a file? ▻

Problem with realloc()

In article <2dlf17$565@morrow.stanford.edu>,
Patrick L. Nolan <pln@egret0.Stanford.EDU> wrote:
>I'm having a problem with realloc().  I hope it's just a bonehead
>error on my part.  This is on a Sun Sparc with SunOS 4.1.1.
>[...]

Various followups have suggested (erroneously) that this is due
to an incorrect use of the null pointer.  (This despite the fact
that comp.lang.c readers have access to an extremely full and clear
explanation of the mysteries surrounding the null pointer in the form of
the FAQ.)

The correct answer is that the C library shipped with SunOS 4 is not
conformant with ANSI C.  (This shouldn't come as a surprise, given that
the C compiler shipped with the system doesn't grok prototypes.)  One
of the deficiencies of the library is that realloc() doesn't behave
correctly when its first argument is the null pointer.

Patrick mentions that he is using Sun's unbundled C compiler; although
I am not familiar with this product, it was my belief that it is ANSI
conformant.  There are a number of possibilities, including: i) my
belief is wrong; ii) the compiler is ANSI conformant, but the library is
not; or iii) the library has not been correctly installed.

The (imperfect) solution I use is this.

    /* Horrid hack to get ANSI realloc semantics.  Note that this macro
    may evaluate its first argument twice. */

    void *(*sunrealloc)(void *, int) = &realloc;
    #define realloc(p, n) ((p) ? sunrealloc((p), (n)) : malloc(n))

Tim.
--
Tim Goodwin | "Alvestrand's equality: gateways = pain"
PIPEX Ltd   |     -- Harald T Alvestrand.

Original headers:

From: tim@pipex.net (Tim Goodwin)
Newsgroups: comp.lang.c
Subject: Re: Problem with realloc()
Date: 3 Dec 1993 17:24:26 -0000
Organization: PIPEX Ltd, Cambridge, UK.
Message-ID: <2dnsoa$kv5@tank.pipex.net>
References: <2dlf17$565@morrow.stanford.edu>

△ Comp.lang.c △

How to write an EOF in a file? ▻