Re: malloc (0)

From: Maxime Henrion (mux_at_freebsd.org)
Date: 02/21/04

  • Next message: Petri Helenius: "Re: malloc (0)"
    Date: Sat, 21 Feb 2004 18:06:35 +0100
    To: Petri Helenius <pete@rommon.com>
    
    

    Petri Helenius wrote:
    >
    > I?m asking the net list because I came across this when browsing through
    > the networking / resolver code.
    >
    > The question is if the code should check for zero value before calling
    > malloc or is malloc(0) legal if the pointer is never used?
    >
    > I came across this when using dmalloc library and it started complaining
    > about zero sized allocations from get_addrselectpolicy.

    In C99, malloc(0) is legal. From n869.txt :

    %%
    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.
    %%

    As a side note, free(NULL) is also legal in C99.

    Cheers,
    Maxime
    _______________________________________________
    freebsd-net@freebsd.org mailing list
    http://lists.freebsd.org/mailman/listinfo/freebsd-net
    To unsubscribe, send any mail to "freebsd-net-unsubscribe@freebsd.org"


  • Next message: Petri Helenius: "Re: malloc (0)"

    Relevant Pages

    • Re: malloc (0)
      ... If the size of the space requested is zero, ... except that the returned pointer shall not be ... except that the returned pointer shall not be ... mallocisn't allowed to launch missiles either. ...
      (comp.lang.c)
    • Re: Degenerate strcmp
      ... Memory management functions ... If the size of the space requested is zero, ... except that the returned pointer shall ...
      (comp.lang.c)
    • Re: (MS-)DOS PC on a microcontroller??
      ... memory block of zero bytes. ... The pointer returned if the ... Each such allocation shall yield a pointer to ... support malloc/calloc requests for 0 bytes and whether or not the ...
      (comp.arch.embedded)
    • Re: (MS-)DOS PC on a microcontroller??
      ... memory block of zero bytes. ... all of malloc, realloc, and calloc and thus is isolated in the ... The pointer returned if the ... Each such allocation shall yield a pointer to ...
      (comp.arch.embedded)
    • Re: gcc knows about malloc()
      ... I multiplied by zero to compensate". ... No. Multiplying by zero is correct in math (so my analogy is not ... a function through a pointer of the wrong type. ... your program contains a single pointer conversion: ...
      (comp.lang.c)