question on malloc(0)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sri Harsha Dandibhotla

    question on malloc(0)

    int *ptr = malloc(0);
    ptr = realloc(ptr, sizeof(int));

    is this valid?
  • Walter Roberson

    #2
    Re: question on malloc(0)

    In article <660a816a-fdd6-491f-9e34-bd3ff9ae2476@k3 7g2000hsf.googl egroups.com>,
    Sri Harsha Dandibhotla <harsha.dsh@gma il.comwrote:
    >int *ptr = malloc(0);
    >ptr = realloc(ptr, sizeof(int));
    >is this valid?
    Valid under which standard? C89/C90 or C99?

    --
    "Is there any thing whereof it may be said, See, this is new? It hath
    been already of old time, which was before us." -- Ecclesiastes

    Comment

    • Richard Heathfield

      #3
      Re: question on malloc(0)

      Sri Harsha Dandibhotla said:
      int *ptr = malloc(0);
      ptr = realloc(ptr, sizeof(int));
      >
      is this valid?
      Yes, it's perfectly legal, whether or not the original malloc returns NULL
      - but it is unwise if your code will ever have to work under C/370 on MVS.

      Note that, in general, you ought to use a spare pointer when capturing the
      result of realloc, just in case it fails. In this case, though, it hardly
      matters.

      --
      Richard Heathfield <http://www.cpax.org.uk >
      Email: -http://www. +rjh@
      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
      "Usenet is a strange place" - dmr 29 July 1999

      Comment

      • vippstar@gmail.com

        #4
        Re: question on malloc(0)

        On Jul 9, 4:55 pm, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
        wrote:
        In article <660a816a-fdd6-491f-9e34-bd3ff9ae2...@k3 7g2000hsf.googl egroups.com>,
        Sri Harsha Dandibhotla <harsha....@gma il.comwrote:
        >
        int *ptr = malloc(0);
        ptr = realloc(ptr, sizeof(int));
        is this valid?
        >
        Valid under which standard? C89/C90 or C99?
        What happends in each standard with 'malloc(0)'? The wording is not
        clear, '0' is not mentioned as a special case.
        All ISO C99 says is that it allocates 'size' bytes and returns NULL or
        the pointer.
        POSIX says 0 is allowed to return a unique pointer or NULL.

        Comment

        • pete

          #5
          Re: question on malloc(0)

          Walter Roberson wrote:
          In article <660a816a-fdd6-491f-9e34-bd3ff9ae2476@k3 7g2000hsf.googl egroups.com>,
          Sri Harsha Dandibhotla <harsha.dsh@gma il.comwrote:
          >
          >int *ptr = malloc(0);
          >ptr = realloc(ptr, sizeof(int));
          >
          >is this valid?
          Yes.
          Valid under which standard? C89/C90 or C99?
          It makes no difference.

          --
          pete

          Comment

          • Richard Heathfield

            #6
            Re: question on malloc(0)

            vippstar@gmail. com said:

            <snip>
            What happends in each standard with 'malloc(0)'? The wording is not
            clear, '0' is not mentioned as a special case
            Yes, it is:

            C89: "If the size of the space requested is zero, the behavior is
            implementation-defined; the value returned shall be either a null
            pointer or a unique pointer."

            C99: "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."

            --
            Richard Heathfield <http://www.cpax.org.uk >
            Email: -http://www. +rjh@
            Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
            "Usenet is a strange place" - dmr 29 July 1999

            Comment

            • vippstar@gmail.com

              #7
              Re: question on malloc(0)

              On Jul 10, 2:07 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
              vipps...@gmail. com said:
              >
              <snip>
              >
              What happends in each standard with 'malloc(0)'? The wording is not
              clear, '0' is not mentioned as a special case
              >
              Yes, it is:
              >
              C89: "If the size of the space requested is zero, the behavior is
              implementation-defined; the value returned shall be either a null
              pointer or a unique pointer."
              >
              C99: "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."
              Whoops, you are right. That is in 7.20.3 Memory management functions
              (n1256.pdf), and I was looking only at 7.20.3.3 "The malloc function"
              & 7.20.3.4 "The realloc function".

              Comment

              • pete

                #8
                Re: question on malloc(0)

                vippstar@gmail. com wrote:
                On Jul 10, 2:07 am, Richard Heathfield <r...@see.sig.i nvalidwrote:
                >vipps...@gmail .com said:
                >>
                ><snip>
                >>
                >>What happends in each standard with 'malloc(0)'? The wording is not
                >>clear, '0' is not mentioned as a special case
                >Yes, it is:
                >>
                >C89: "If the size of the space requested is zero, the behavior is
                >implementati on-defined; the value returned shall be either a null
                >pointer or a unique pointer."
                >>
                >C99: "If the size of the space requested is zero, the behavior is
                >implementati on-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."
                >
                Whoops, you are right. That is in 7.20.3 Memory management functions
                (n1256.pdf), and I was looking only at 7.20.3.3 "The malloc function"
                & 7.20.3.4 "The realloc function".

                You have to read the introduction to the library,
                especially
                7.1.1 Definitions of terms
                7.1.4 Use of library functions
                as well as the beginning of the header description
                of the function that you're interested in,
                to really understand the function descriptions
                in the standard.
                Most of that, is not difficult reading.

                --
                pete

                Comment

                • Barry Schwarz

                  #9
                  Re: question on malloc(0)

                  On Wed, 09 Jul 2008 14:01:15 +0000, Richard Heathfield
                  <rjh@see.sig.in validwrote:
                  >Sri Harsha Dandibhotla said:
                  >
                  >int *ptr = malloc(0);
                  >ptr = realloc(ptr, sizeof(int));
                  >>
                  >is this valid?
                  >
                  >Yes, it's perfectly legal, whether or not the original malloc returns NULL
                  >- but it is unwise if your code will ever have to work under C/370 on MVS.
                  What is special about C/370 or MVS (since that is where I spend about
                  a third of each workday) that makes the code more unwise there than on
                  a typical desktop system?


                  Remove del for email

                  Comment

                  • Richard Heathfield

                    #10
                    Re: question on malloc(0)

                    Barry Schwarz said:
                    On Wed, 09 Jul 2008 14:01:15 +0000, Richard Heathfield
                    <rjh@see.sig.in validwrote:
                    >
                    >>Sri Harsha Dandibhotla said:
                    >>
                    >>int *ptr = malloc(0);
                    >>ptr = realloc(ptr, sizeof(int));
                    >>>
                    >>is this valid?
                    >>
                    >>Yes, it's perfectly legal, whether or not the original malloc returns
                    >>NULL - but it is unwise if your code will ever have to work under C/370
                    >>on MVS.
                    >
                    What is special about C/370 or MVS (since that is where I spend about
                    a third of each workday) that makes the code more unwise there than on
                    a typical desktop system?
                    On that platform, malloc(0) hangs the process - or at least it used to, a
                    few years back. (This is not one of the permitted outcomes of malloc(0),
                    so it counts as an implementation bug.)

                    --
                    Richard Heathfield <http://www.cpax.org.uk >
                    Email: -http://www. +rjh@
                    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                    "Usenet is a strange place" - dmr 29 July 1999

                    Comment

                    • CBFalconer

                      #11
                      Re: question on malloc(0)

                      pete wrote:
                      Walter Roberson wrote:
                      >Sri Harsha Dandibhotla <harsha.dsh@gma il.comwrote:
                      >>
                      >>int *ptr = malloc(0);
                      >>ptr = realloc(ptr, sizeof(int));
                      >>>
                      >>is this valid?
                      >
                      Yes.
                      >
                      >Valid under which standard? C89/C90 or C99?
                      >
                      It makes no difference.
                      However if malloc(0) on the system returns a non-NULL pointer it
                      may constitute a memory leak.

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


                      Comment

                      • Gordon Burditt

                        #12
                        Re: question on malloc(0)

                        free(malloc(0)) is not a memory leak (and regardless of what malloc(0)
                        returns, it's still acceptable to free() ). If you forget to free()
                        the pointer returned by malloc(n), it's a memory leak regardless
                        of whether n == 0 or not.


                        Comment

                        • lawrence.jones@siemens.com

                          #13
                          Re: question on malloc(0)

                          vippstar@gmail. com wrote:
                          >
                          Whoops, you are right. That is in 7.20.3 Memory management functions
                          (n1256.pdf), and I was looking only at 7.20.3.3 "The malloc function"
                          & 7.20.3.4 "The realloc function".
                          When consulting the C standard, *always* look at all the parents of the
                          particular subclause you're interested in; they frequently contain
                          valuable additional information.
                          --
                          Larry Jones

                          Nobody knows how to pamper like a Mom. -- Calvin

                          Comment

                          • lawrence.jones@siemens.com

                            #14
                            Re: question on malloc(0)

                            Richard Heathfield <rjh@see.sig.in validwrote:
                            >
                            Yes, it's perfectly legal, whether or not the original malloc returns NULL
                            - but it is unwise if your code will ever have to work under C/370 on MVS.
                            Would you care to expand on that?
                            --
                            Larry Jones

                            ANY idiot can be famous. I figure I'm more the LEGENDARY type! -- Calvin

                            Comment

                            • Richard Heathfield

                              #15
                              Re: question on malloc(0)

                              lawrence.jones@ siemens.com said:
                              Richard Heathfield <rjh@see.sig.in validwrote:
                              >>
                              >Yes, it's perfectly legal, whether or not the original malloc returns
                              >NULL - but it is unwise if your code will ever have to work under C/370
                              >on MVS.
                              >
                              Would you care to expand on that?
                              malloc(0) used to cause an abend on that platform. Whether it still does, I
                              have no idea.

                              --
                              Richard Heathfield <http://www.cpax.org.uk >
                              Email: -http://www. +rjh@
                              Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
                              "Usenet is a strange place" - dmr 29 July 1999

                              Comment

                              Working...