Re: realloc, need to free old ptr?

Discussion in 'C Programming' started by Christopher Benson-Manica, Aug 26, 2003.

  1. Tony Finch <> spoke thus: (on clc.moderated...)

    > Note that you should avoid calling realloc() with a size of 0
    > since the semantics are ambiguous.


    This isn't correct, is it? What does the standard say about it?

    --
    Christopher Benson-Manica | Jumonji giri, for honour.
    ataru(at)cyberspace.org |
     
    Christopher Benson-Manica, Aug 26, 2003
    #1
    1. Advertising

  2. Christopher Benson-Manica <> writes:

    > Tony Finch <> spoke thus: (on clc.moderated...)
    >
    > > Note that you should avoid calling realloc() with a size of 0
    > > since the semantics are ambiguous.

    >
    > This isn't correct, is it? What does the standard say about it?


    7.20.3#1: [...] If the size of the space requested is zero, the behavior
    is implementation-defined: either a null pointer is returned, or the
    behavior is as if the size were some nonzero value, except that the
    returned pointer shall not be used to access an object.

    Martin
     
    Martin Dickopp, Aug 26, 2003
    #2
    1. Advertising

  3. Martin Dickopp <> spoke thus:

    > 7.20.3#1: [...] If the size of the space requested is zero, the behavior
    > is implementation-defined: either a null pointer is returned, or the
    > behavior is as if the size were some nonzero value, except that the
    > returned pointer shall not be used to access an object.


    I see... so is the admonition that the pointer returned should not be used to
    access an object directed at the compiler or the programmer? Or both? And
    what is the motivation for allowing either of these behaviors?

    --
    Christopher Benson-Manica | Jumonji giri, for honour.
    ataru(at)cyberspace.org |
     
    Christopher Benson-Manica, Aug 26, 2003
    #3
  4. Christopher Benson-Manica

    Malcolm Guest

    "Christopher Benson-Manica" <> wrote
    [ realloc of 0 ]
    > I see... so is the admonition that the pointer returned should not be
    > used to access an object directed at the compiler or the programmer?
    >

    Programmer. A compiler is at liberty to issue a diagnostic if such a pointer
    is dereferenced, but it practise it would be too difficult to get a compiler
    to do so, because the expression passed to realloc() will probably be
    evaluated at run-time.
    >
    > Or both? And what is the motivation for allowing either of these
    > behaviors?
    >

    It depends how malloc() is implemented. Sometimes it is natural to return a
    NULL pointer if no memory is requested, other times the natural thing to do
    is to return a dummy block of no memory.
    ANSI could have insisted on one behaviour or the other, but they didn't, for
    reasons above my level.
     
    Malcolm, Aug 26, 2003
    #4
  5. On 26 Aug 2003 19:17:02 +0200, Martin Dickopp
    <> wrote:
    <snap>
    > > And what is the motivation for allowing either of these behaviors?

    >
    > This is described in the C rationale
    > <http://std.dkuug.dk/JTC1/SC22/WG14/www/docs/n897.pdf>.
    >

    Superseded by n937 in general, although no changes re 7.20.3.

    - David.Thompson1 at worldnet.att.net
     
    Dave Thompson, Sep 1, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sid
    Replies:
    5
    Views:
    1,089
  2. Heiko Vogel
    Replies:
    3
    Views:
    575
    Method Man
    Sep 14, 2004
  3. franco ziade

    const ptr to const ptr ?

    franco ziade, Feb 17, 2005, in forum: C Programming
    Replies:
    3
    Views:
    406
    franco ziade
    Feb 17, 2005
  4. G Fernandes
    Replies:
    9
    Views:
    608
    DHOLLINGSWORTH2
    Feb 27, 2005
  5. James Harris

    Why not realloc(&ptr, ...) and free(&ptr)?

    James Harris, Aug 5, 2013, in forum: C Programming
    Replies:
    31
    Views:
    536
    Tim Rentsch
    Aug 8, 2013
Loading...

Share This Page