Can realloc(p,0) return NULL when p is non-NULL and memory aplenty?

Discussion in 'C Programming' started by Francois Grieu, Mar 6, 2008.

  1. When running the following code under MinGW, I get
    realloc(p,0) returned NULL
    Is that a non-conformance?

    TIA,
    Francois Grieu

    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
    void *p;
    p = malloc(0);
    if (p==NULL)
    puts("malloc(0) returned NULL");
    else
    {
    p = realloc(p,0);
    if (p==NULL)
    puts("realloc(p,0) returned NULL");
    else
    puts("realloc(p,0) did not return NULL");
    }
    return 0;
    }
    Francois Grieu, Mar 6, 2008
    #1
    1. Advertising

  2. Francois Grieu wrote:
    > When running the following code under MinGW, I get
    > realloc(p,0) returned NULL
    > Is that a non-conformance?

    Whether malloc(0) returns NULL or a pointerr to 0 bytes is implementation
    defined
    I think the same is true for realloc(p, 0)

    Bye, Jojo
    Joachim Schmitz, Mar 6, 2008
    #2
    1. Advertising

  3. In article <>,
    Francois Grieu <> wrote:
    >When running the following code under MinGW, I get
    >realloc(p,0) returned NULL
    >Is that a non-conformance?


    No, it is conformance, and returning non-NULL would be non-conformance.

    C89 4.10.3.4 The realloc Funciton

    If size is zero and ptr is not a null pointer, the object
    it points to is freed.

    Returns

    The realloc function returns either a null pointer or a
    pointer to the possibly moved allocated space.


    In the case of zero as the size, there is no more allocated space
    (because zero size requires freeing), so the other branch of the
    'either' kicks in, requiring realloc to return a null pointer
    for this case.
    --
    amazon.com's top 8 books about "walter" are Kotzwinkle/ Gundy/ Colman's
    "Walter the Farting Dog"
    Walter Roberson, Mar 6, 2008
    #3
  4. Walter Roberson wrote:
    > In article
    > <>,
    > Francois Grieu <> wrote:
    >> When running the following code under MinGW, I get
    >> realloc(p,0) returned NULL
    >> Is that a non-conformance?

    >
    > No, it is conformance, and returning non-NULL would be
    > non-conformance.
    >
    > C89 4.10.3.4 The realloc Funciton
    >
    > If size is zero and ptr is not a null pointer, the object
    > it points to is freed.
    >
    > Returns
    >
    > The realloc function returns either a null pointer or a
    > pointer to the possibly moved allocated space.
    >
    >
    > In the case of zero as the size, there is no more allocated space
    > (because zero size requires freeing), so the other branch of the
    > 'either' kicks in, requiring realloc to return a null pointer
    > for this case.

    From n1256:
    7.20.3 Memory management functions

    1 The order and contiguity of storage allocated by successive calls to the
    calloc,
    malloc, and realloc functions is unspecified. The pointer returned if the
    allocation
    succeeds is suitably aligned so that it may be assigned to a pointer to any
    type of object
    and then used to access such an object or an array of such objects in the
    space allocated
    (until the space is explicitly deallocated). The lifetime of an allocated
    object extends
    from the allocation until the deallocation. Each such allocation shall yield
    a pointer to an
    object disjoint from any other object. The pointer returned points to the
    start (lowest byte
    address) of the allocated space. If the space cannot be allocated, a null
    pointer is
    returned. If the size of the space requested is zero, the behavior is
    implementationdefined:
    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.

    ....

    7.20.3.4 The realloc function
    ....
    3 If ptr is a null pointer, the realloc function behaves like the malloc
    function for the
    specified size



    So it surely seems implementation defined to me.



    Bye, Jojo
    Joachim Schmitz, Mar 6, 2008
    #4
  5. Joachim Schmitz wrote:
    > Walter Roberson wrote:
    >> In article
    >> <>,
    >> Francois Grieu <> wrote:
    >>> When running the following code under MinGW, I get
    >>> realloc(p,0) returned NULL
    >>> Is that a non-conformance?

    >>
    >> No, it is conformance, and returning non-NULL would be
    >> non-conformance.
    >>
    >> C89 4.10.3.4 The realloc Funciton
    >>
    >> If size is zero and ptr is not a null pointer, the object
    >> it points to is freed.
    >>
    >> Returns
    >>
    >> The realloc function returns either a null pointer or a
    >> pointer to the possibly moved allocated space.
    >>
    >>
    >> In the case of zero as the size, there is no more allocated space
    >> (because zero size requires freeing), so the other branch of the
    >> 'either' kicks in, requiring realloc to return a null pointer
    >> for this case.

    > From n1256:
    > 7.20.3 Memory management functions
    >
    > 1 The order and contiguity of storage allocated by successive calls
    > to the calloc,
    > malloc, and realloc functions is unspecified. The pointer returned if
    > the allocation
    > succeeds is suitably aligned so that it may be assigned to a pointer
    > to any type of object
    > and then used to access such an object or an array of such objects in
    > the space allocated
    > (until the space is explicitly deallocated). The lifetime of an
    > allocated object extends
    > from the allocation until the deallocation. Each such allocation
    > shall yield a pointer to an
    > object disjoint from any other object. The pointer returned points to
    > the start (lowest byte
    > address) of the allocated space. If the space cannot be allocated, a
    > null pointer is
    > returned. If the size of the space requested is zero, the behavior is
    > implementationdefined:
    > 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.
    >
    > ...
    >
    > 7.20.3.4 The realloc function
    > ...
    > 3 If ptr is a null pointer, the realloc function behaves like the
    > malloc function for the
    > specified size

    Oops, that section is irrelevant...

    > So it surely seems implementation defined to me.

    But from the 1st section I think this still holds true

    Bye, Jojo
    Joachim Schmitz, Mar 6, 2008
    #5
  6. Joachim Schmitz wrote:
    > Joachim Schmitz wrote:
    >> Walter Roberson wrote:
    >>> In article
    >>> <>,
    >>> Francois Grieu <> wrote:
    >>>> When running the following code under MinGW, I get
    >>>> realloc(p,0) returned NULL
    >>>> Is that a non-conformance?
    >>>
    >>> No, it is conformance, and returning non-NULL would be
    >>> non-conformance.
    >>>
    >>> C89 4.10.3.4 The realloc Funciton
    >>>
    >>> If size is zero and ptr is not a null pointer, the object
    >>> it points to is freed.
    >>>
    >>> Returns
    >>>
    >>> The realloc function returns either a null pointer or a
    >>> pointer to the possibly moved allocated space.
    >>>
    >>>
    >>> In the case of zero as the size, there is no more allocated space
    >>> (because zero size requires freeing), so the other branch of the
    >>> 'either' kicks in, requiring realloc to return a null pointer
    >>> for this case.

    >> From n1256:
    >> 7.20.3 Memory management functions
    >>
    >> 1 The order and contiguity of storage allocated by successive calls
    >> to the calloc,
    >> malloc, and realloc functions is unspecified. The pointer returned if
    >> the allocation
    >> succeeds is suitably aligned so that it may be assigned to a pointer
    >> to any type of object
    >> and then used to access such an object or an array of such objects in
    >> the space allocated
    >> (until the space is explicitly deallocated). The lifetime of an
    >> allocated object extends
    >> from the allocation until the deallocation. Each such allocation
    >> shall yield a pointer to an
    >> object disjoint from any other object. The pointer returned points to
    >> the start (lowest byte
    >> address) of the allocated space. If the space cannot be allocated, a
    >> null pointer is
    >> returned. If the size of the space requested is zero, the behavior is
    >> implementationdefined:
    >> 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.
    >>
    >> ...
    >>
    >> 7.20.3.4 The realloc function
    >> ...
    >> 3 If ptr is a null pointer, the realloc function behaves like the
    >> malloc function for the
    >> specified size

    > Oops, that section is irrelevant...
    >
    >> So it surely seems implementation defined to me.

    > But from the 1st section I think this still holds true

    Esp. as the section Walter quoted isn't in n1256 (anymore?)

    Bye, Jojo
    Joachim Schmitz, Mar 6, 2008
    #6
  7. Re: Can realloc(p,0) return NULL when p is non-NULL and memoryaplenty?

    On Thu, 06 Mar 2008 18:03:02 +0000, Walter Roberson wrote:
    > In article
    > <>,
    > Francois Grieu <> wrote:
    >>When running the following code under MinGW, I get realloc(p,0) returned
    >>NULL
    >>Is that a non-conformance?

    >
    > No, it is conformance, and returning non-NULL would be non-conformance.
    >
    > [C89 citation snipped]


    While you're not at all wrong, please keep in mind that this is one of the
    areas in which C99 differs from the previous standard. In C99, it's
    unspecified whether realloc(p, 0) returns a null pointer, but if it
    returns a null pointer, then p is *not* freed.
    Harald van Dijk, Mar 6, 2008
    #7
  8. Re: Can realloc(p,0) return NULL when p is non-NULL and memory aplenty?

    Harald van D?k wrote:
    > On Thu, 06 Mar 2008 18:03:02 +0000, Walter Roberson wrote:
    >> In article
    >> <>,
    >> Francois Grieu <> wrote:
    >>> When running the following code under MinGW, I get realloc(p,0)
    >>> returned NULL
    >>> Is that a non-conformance?

    >>
    >> No, it is conformance, and returning non-NULL would be
    >> non-conformance.
    >>
    >> [C89 citation snipped]

    >
    > While you're not at all wrong, please keep in mind that this is one
    > of the areas in which C99 differs from the previous standard. In C99,
    > it's unspecified whether realloc(p, 0) returns a null pointer, but if
    > it returns a null pointer, then p is *not* freed.

    I don't read it mlike this

    The realloc function returns a pointer to the new object (which may have the
    same
    value as a pointer to the old object), or a null pointer if the new object
    could not be
    allocated.

    allocating 0 bytes can't be too difficult ;-), so no compelling reason to
    return NULL

    Bye, Jojo
    Joachim Schmitz, Mar 6, 2008
    #8
  9. Re: Can realloc(p,0) return NULL when p is non-NULL and memoryaplenty?

    In article <da591$47d036a0$541dfcd3$1.nb.home.nl>,
    Harald van =?UTF-8?b?RMSzaw==?= <> wrote:
    >On Thu, 06 Mar 2008 18:03:02 +0000, Walter Roberson wrote:
    >> In article
    >> <>,
    >> Francois Grieu <> wrote:
    >>>When running the following code under MinGW, I get realloc(p,0) returned
    >>>NULL
    >>>Is that a non-conformance?


    >> No, it is conformance, and returning non-NULL would be non-conformance.


    >> [C89 citation snipped]


    >While you're not at all wrong, please keep in mind that this is one of the
    >areas in which C99 differs from the previous standard. In C99, it's
    >unspecified whether realloc(p, 0) returns a null pointer, but if it
    >returns a null pointer, then p is *not* freed.


    Good point -- but as best I -recall- MinGW is not C99.

    --
    "Beware of bugs in the above code; I have only proved it correct,
    not tried it." -- Donald Knuth
    Walter Roberson, Mar 6, 2008
    #9
  10. Re: Can realloc(p,0) return NULL when p is non-NULL and memoryaplenty?

    On Mar 6, 7:03 pm, -cnrc.gc.ca (Walter Roberson)
    wrote:
    > Francois Grieu <> wrote:
    >
    >> When running the following code under MinGW, I get
    >> realloc(p,0) returned NULL
    >> Is that a non-conformance?

    >
    > No, it is conformance, and returning non-NULL would be non-conformance.


    I disagree (at least) about the second member of that answer under
    C99. IMHO realloc(p,0) is allowed to return the same thing as
    malloc(0), which is not required to return NULL.

    I reason that when p is a valid non-NULL pointer to a pointer returned
    by malloc() and memory is aplenty, realloc(p,0) must return a pointer
    to a new object that has the size 0, which is the same thing a what
    malloc(0) should return, which is not required to be NULL, and is not
    NULL in many implementations, including that one I'm strugling with.

    > C89 4.10.3.4 The realloc Function
    >
    > If size is zero and ptr is not a null pointer, the object
    > it points to is freed.
    >
    > Returns
    >
    > The realloc function returns either a null pointer or a
    > pointer to the possibly moved allocated space.
    >
    > In the case of zero as the size, there is no more allocated space
    > (because zero size requires freeing), so the other branch of the
    > 'either' kicks in, requiring realloc to return a null pointer
    > for this case.



    C99 says {
    void *realloc(void *ptr, size_t size);

    The realloc function deallocates the old object pointed to by ptr and
    returns a pointer to a new object that has the size specified by size.
    The contents of the new object shall be the same as that of the old
    object prior to deallocation, up to the lesser of the new and old
    sizes. Any bytes in the new object beyond the size of the old object
    have indeterminate values.

    If ptr is a null pointer, the realloc function behaves like the malloc
    function for the specified size. Otherwise, if ptr does not match a
    pointer earlier returned by the calloc, malloc, or realloc function,
    or if the space has been deallocated by a call to the free or realloc
    function, the behavior is undefined. If memory for the new object
    cannot be allocated, the old object is not deallocated and its value
    is unchanged.

    The realloc function returns a pointer to the new object (which may
    have the same value as a pointer to the old object), or a null pointer
    if the new object could not be allocated.
    }




    I get the same result "realloc(p,0) returned NULL" with the source
    changed to the deeper test:

    #include <stdio.h>
    #include <stdlib.h>

    int main(void)
    {
    void *p = malloc(1);
    if (p==NULL)
    puts("Memory is very low"); // does not occur
    if (malloc(0)==NULL)
    puts("malloc(0) returned NULL"); // does not occur
    if (realloc(NULL,0)==NULL)
    puts("realloc(NULL,0) returned NULL"); // does not occur
    if (realloc(p,0)==NULL)
    puts("realloc(p,0) returned NULL"); // occurs ?!
    if (malloc(10000)==NULL)
    puts("Memory was rather low"); // does not occur
    }


    So I get an implementation of realloc(ptr,0) that turns a NULL ptr to
    a non-NULL result, and non-NULL ptr to NULL result; which is at the
    very least a surprise, non-orthogonal, and error-prone. In my case a
    perfectly sound-looking "read the whole file in memory, in several
    chuncks as necessary" function failed for an empty file, claiming
    there is no memory.

    Francois Grieu
    Francois Grieu, Mar 6, 2008
    #10
  11. Re: Can realloc(p,0) return NULL when p is non-NULL and memoryaplenty?

    On Thu, 06 Mar 2008 19:31:30 +0100, Joachim Schmitz wrote:
    > Harald van D?k wrote:
    >> On Thu, 06 Mar 2008 18:03:02 +0000, Walter Roberson wrote:
    >>> In article
    >>> <>,
    >>> Francois Grieu <> wrote:
    >>>> When running the following code under MinGW, I get realloc(p,0)
    >>>> returned NULL
    >>>> Is that a non-conformance?
    >>>
    >>> No, it is conformance, and returning non-NULL would be
    >>> non-conformance.
    >>>
    >>> [C89 citation snipped]

    >>
    >> While you're not at all wrong, please keep in mind that this is one of
    >> the areas in which C99 differs from the previous standard. In C99, it's
    >> unspecified whether realloc(p, 0) returns a null pointer, but if it
    >> returns a null pointer, then p is *not* freed.

    > I don't read it mlike this
    >
    > The realloc function returns a pointer to the new object (which may have
    > the same
    > value as a pointer to the old object), or a null pointer if the new
    > object could not be
    > allocated.


    7.20.3p1 applies to all allocation functions.
    "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."

    This allows realloc(p, 0) to unconditionally fail (since the text you
    quoted specifies that a null pointer return value signifies failure for
    realloc).

    > allocating 0 bytes can't be too difficult ;-)


    <nit>realloc(p, 0), if it succeeded, hasn't allocated 0 bytes. It has
    allocated one or more bytes, plus whatever information free needs to give
    it back later.</nit>

    Anyway, even if it weren't specifically allowed to always fail, it could
    still for example fail on implementations where if the size is small,
    *alloc returns pointers to pre-allocated buckets depending on the
    requested size.
    Harald van Dijk, Mar 6, 2008
    #11
  12. Francois Grieu

    CBFalconer Guest

    Walter Roberson wrote:
    > Francois Grieu <> wrote:
    >
    >> When running the following code under MinGW, I get
    >> realloc(p,0) returned NULL
    >> Is that a non-conformance?

    >
    > No, it is conformance, and returning non-NULL would be non-conformance.
    >

    .... snip C89 quote ...

    C99 quote:

    7.20.3 Memory management functions

    [#1] The order and contiguity of storage allocated by
    successive calls to the calloc, malloc, and realloc
    functions is unspecified. The pointer returned if the
    allocation succeeds is suitably aligned so that it may be
    assigned to a pointer to any type of object and then used to
    access such an object or an array of such objects in the
    space allocated (until the space is explicitly freed or
    reallocated). Each such allocation shall yield a pointer to
    an object disjoint from any other object. The pointer
    returned points to the start (lowest byte address) of the
    allocated space. If the space cannot be allocated, a null
    pointer is returned. 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. The value of a pointer <
    that refers to freed space is indeterminate. <

    Note that either a NULL or an individual pointer can be returned.
    The pointer to a zero size space can't be dereferenced. You also
    can't positively tell a malloc system error from success. So I
    advise always allocating (or reallocing) 1 or more bytes.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Mar 6, 2008
    #12
  13. Francois Grieu wrote:
    > When running the following code under MinGW, I get
    > realloc(p,0) returned NULL
    > Is that a non-conformance?
    >
    > TIA,
    > Francois Grieu
    >
    > #include <stdio.h>
    > #include <stdlib.h>
    >
    > int main(void)
    > {
    > void *p;
    > p = malloc(0);
    > if (p==NULL)
    > puts("malloc(0) returned NULL");
    > else
    > {
    > p = realloc(p,0);
    > if (p==NULL)
    > puts("realloc(p,0) returned NULL");
    > else
    > puts("realloc(p,0) did not return NULL");
    > }
    > return 0;
    > }


    In the UNIX manpage for the function, you will find the following text:

    realloc() returns a pointer to the newly allocated memory, which is
    suitably aligned for any kind of variable and may be different from
    ptr, or NULL if the request fails. If size was equal to 0, either
    NULL or a pointer suitable to be passed to free() is returned. If
    realloc() fails the original block is left untouched; it is not freed or
    moved.

    Now, it really does beg the question why you are trying to realloc() the
    void * to be 0 bytes long. The function you should use for this is
    free() (and you will find in the same manpage that realloc(x, 0) and
    free(x) are equivalent.

    Nevertheless, that behaviour is defined and correct.

    --
    --Falcon Kirtaran
    Falcon Kirtaran, Mar 7, 2008
    #13
  14. CBFalconer wrote:
    <snip>
    > You also can't positively tell a malloc system error from success.

    Probably not with standrd C, but AFAIK POSIX reqires malloc to set errno on
    failure.
    So if malloc() return NULL, lookup errno, if that is 0, no failure occured.

    Bye, Jojo
    Joachim Schmitz, Mar 7, 2008
    #14
  15. Francois Grieu

    Micah Cowan Guest

    CBFalconer <> writes:

    > Note that either a NULL or an individual pointer can be returned.
    > The pointer to a zero size space can't be dereferenced. You also
    > can't positively tell a malloc system error from success. So I
    > advise always allocating (or reallocing) 1 or more bytes.


    While that's true, I'm not sure I see what practical use it would make
    to be able to determine the difference between NULL returned because
    of a system error, and NULL returned because you asked for either that
    or an unusable pointer.

    That being said, I agree about reallocing 1 or more bytes, since doing
    0 without knowing that your pointer will be freed is pretty useless.

    --
    Micah J. Cowan
    Programmer, musician, typesetting enthusiast, gamer...
    http://micah.cowan.name/
    Micah Cowan, Mar 7, 2008
    #15
    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. Jonas
    Replies:
    21
    Views:
    1,105
    Mark McIntyre
    Oct 14, 2003
  2. Jeff Rodriguez

    Can realloc potentially cause a memory leak?

    Jeff Rodriguez, Nov 19, 2003, in forum: C Programming
    Replies:
    4
    Views:
    317
    Derk Gwen
    Nov 19, 2003
  3. M a n i s h

    how the realloc( ) allocates memory ?

    M a n i s h, Nov 21, 2003, in forum: C Programming
    Replies:
    6
    Views:
    497
    Floyd Davidson
    Nov 21, 2003
  4. Carl
    Replies:
    21
    Views:
    986
    Patricia Shanahan
    Aug 24, 2006
  5. subramanian

    realloc return value

    subramanian, Dec 20, 2006, in forum: C Programming
    Replies:
    12
    Views:
    2,581
Loading...

Share This Page