malloc (0)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bhaskar

    #1

    malloc (0)

    Hey can any one tell me what malloc 0 does, it assigns space in memory
    i know that but is it usable and how many bytes does it allocate ,
    allocation of 0 bytes is difficult-to-digest concept and still in the
    memory.

  • Kiru Sengal

    #2
    Re: malloc (0)


    Bhaskar wrote:
    Hey can any one tell me what malloc 0 does, it assigns space in memory
    i know that but is it usable and how many bytes does it allocate ,
    allocation of 0 bytes is difficult-to-digest concept and still in the
    memory.

    I believe it returns NULL.
    You're asking for 0 (no) memory to be allocated by doing malloc(0).

    Comment

    • Lew Pitcher

      #3
      Re: malloc (0)

      -----BEGIN PGP SIGNED MESSAGE-----
      Hash: SHA1


      Bhaskar wrote:
      Hey can any one tell me what malloc 0 does,
      ITYM
      malloc(0);

      And, the answer is yes and no.
      it assigns space in memory i know that
      Well, you know wrong, then
      but is it usable and how many bytes does it allocate ,
      FWIW, you ask for 0 bytes, you might just get zero bytes
      allocation of 0 bytes is difficult-to-digest concept and still in the
      memory.
      The C standards (actually, the draft) says

      7.20.3 Memory management functions
      [snip]
      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 key phrase is "implementa tion defined". That means that we can't
      answer your questions (or even grant that your assertions are correct
      or not) without knowing the details of the C implementation you are
      using. As I read it, so long as your C compiler/environment's
      implementation documents it, malloc(0) could legitimately launch
      Russian ICBM missles at your house or cause daemons to fly out of your
      nose (the dreaded nasal daemons). Or, it could manipulate its
      underlying storage management components so as to remove zero
      user-accessable bytes of storage from the appropriate pool of free
      storage and place it in a pool of used storage (such an operation would
      likely involve an internal allocation of 0+n bytes, where the n
      represents the data management overhead).

      HTH

      - --
      Lew Pitcher

      -----BEGIN PGP SIGNATURE-----
      Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12

      iD8DBQFEqrmCagV FX4UWr64RAt3pAK CDJTOLbg+Dhrtri NiA4PWSbKbJXACf SrtI
      Axh2N6HHw/XOOkpbvGYYb2g=
      =cE92
      -----END PGP SIGNATURE-----

      Comment

      • Antoine Junod

        #4
        Re: malloc (0)

        "Kiru Sengal" <kiru.sengal@gm ail.comwrites:
        Bhaskar wrote:
        Hey can any one tell me what malloc 0 does, it assigns space in memory
        i know that but is it usable and how many bytes does it allocate ,
        allocation of 0 bytes is difficult-to-digest concept and still in the
        memory.
        >
        I believe it returns NULL.
        I don't think so:

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

        int main(int argc, char **argv){
        int *i = (int *)malloc(0);

        printf("%p\n", (void *)i);
        printf("%p\n", NULL);
        return 0;
        }

        output me:
        toto@epia ~ $ ./a.out
        0x804a050
        (nil)

        Comment

        • Harald van Dijk

          #5
          Re: malloc (0)

          Lew Pitcher wrote:
          Bhaskar wrote:
          Hey can any one tell me what malloc 0 does,
          >
          ITYM
          malloc(0);
          >
          And, the answer is yes and no.
          >
          it assigns space in memory i know that
          >
          Well, you know wrong, then
          >
          but is it usable and how many bytes does it allocate ,
          >
          FWIW, you ask for 0 bytes, you might just get zero bytes
          >
          allocation of 0 bytes is difficult-to-digest concept and still in the
          memory.
          >
          The C standards (actually, the draft) says
          >
          7.20.3 Memory management functions
          [snip]
          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 key phrase is "implementa tion defined". That means that we can't
          answer your questions (or even grant that your assertions are correct
          or not) without knowing the details of the C implementation you are
          using. As I read it, so long as your C compiler/environment's
          implementation documents it, malloc(0) could legitimately launch
          Russian ICBM missles at your house or cause daemons to fly out of your
          nose (the dreaded nasal daemons).
          "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."

          Unless either returning a null pointer by itself in a user function, or
          calling malloc with a nonzero size, is allowed to launch missiles, no,
          malloc(0) isn't allowed to launch missiles either. Either it just
          returns a null pointer, or it behaves (almost) as, for example,
          malloc(1).

          Comment

          • Malcolm

            #6
            Re: malloc (0)




            "Bhaskar" <bhaskar.tilak@ gmail.comwrote in message
            news:1152038573 .171705.164690@ 75g2000cwc.goog legroups.com...
            Hey can any one tell me what malloc 0 does, it assigns space in memory
            i know that but is it usable and how many bytes does it allocate ,
            allocation of 0 bytes is difficult-to-digest concept and still in the
            memory.
            >
            It can either return a pointer to a block of no length, or it can return a
            null pointer.

            A block of no length might seem a slightly Hindu concept, but it is not that
            difficult for anyone to understand, even if they don't apprecuiate all the
            subtleties of zero..
            Imagine a control block of 8 bytes which gieves information about the memory
            allocated, so that free() can return it properly to the pool. malloc returns
            a pointer to the meory immediately after this block.

            Now image we allocate block A, of 4 bytes, and block B od zero bytes, the
            block C of 4 bytes.

            AAAAAAAAaaaaBBB BBBBBCCCCCCCCcc cc
            ^ ^ ^

            The Chinese hats represent the pointers returned. When we call free(),
            internally 8 bytes are subtracted and this tells the system where the
            control information is.




            --
            Buy my book 12 Common Atheist Arguments (refuted)
            $1.25 download or $7.20 paper, available www.lulu.com/bgy1mm


            Comment

            • Keith Thompson

              #7
              Re: malloc (0)

              "Lew Pitcher" <lpitcher@sympa tico.cawrites:
              [...]
              The key phrase is "implementa tion defined". That means that we can't
              answer your questions (or even grant that your assertions are correct
              or not) without knowing the details of the C implementation you are
              using.
              Correct.
              As I read it, so long as your C compiler/environment's
              implementation documents it, malloc(0) could legitimately launch
              Russian ICBM missles at your house or cause daemons to fly out of your
              nose (the dreaded nasal daemons).
              [snip]

              No. It's implementation-defined behavior, not undefined behavior.

              --
              Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
              San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
              We must do something. This is something. Therefore, we must do this.

              Comment

              • Nils O. SelÃ¥sdal

                #8
                Re: malloc (0)

                Keith Thompson wrote:
                "Lew Pitcher" <lpitcher@sympa tico.cawrites:
                [...]
                >The key phrase is "implementa tion defined". That means that we can't
                >answer your questions (or even grant that your assertions are correct
                >or not) without knowing the details of the C implementation you are
                >using.
                >
                Correct.
                >
                > As I read it, so long as your C compiler/environment's
                >implementati on documents it, malloc(0) could legitimately launch
                >Russian ICBM missles at your house or cause daemons to fly out of your
                >nose (the dreaded nasal daemons).
                [snip]
                >
                No. It's implementation-defined behavior, not undefined behavior.
                And in either case, the return value of malloc(0) is safe to pass
                to free.

                Comment

                • Clever Monkey

                  #9
                  Re: malloc (0)

                  Nils O. Selåsdal wrote:
                  Keith Thompson wrote:
                  >"Lew Pitcher" <lpitcher@sympa tico.cawrites:
                  >[...]
                  >>The key phrase is "implementa tion defined". That means that we can't
                  >>answer your questions (or even grant that your assertions are correct
                  >>or not) without knowing the details of the C implementation you are
                  >>using.
                  >>
                  >Correct.
                  >>
                  >> As I read it, so long as your C compiler/environment's
                  >>implementatio n documents it, malloc(0) could legitimately launch
                  >>Russian ICBM missles at your house or cause daemons to fly out of your
                  >>nose (the dreaded nasal daemons).
                  >[snip]
                  >>
                  >No. It's implementation-defined behavior, not undefined behavior.
                  >
                  And in either case, the return value of malloc(0) is safe to pass
                  to free.
                  >
                  True, but this is really about the standard behaviour for free(), rather
                  than anything specific to malloc() or its return. That is, the standard
                  says that when passing a null pointer to free() it does nothing.

                  Comment

                  • Joe Wright

                    #10
                    Re: malloc (0)

                    Bhaskar wrote:
                    Hey can any one tell me what malloc 0 does, it assigns space in memory
                    i know that but is it usable and how many bytes does it allocate ,
                    allocation of 0 bytes is difficult-to-digest concept and still in the
                    memory.
                    >
                    Try this.

                    #include <stdio.h>
                    #include <stdlib.h>
                    int main(void) {
                    void *vp = malloc(0);
                    printf("malloc( 0) returned %p\n", vp);
                    return 0;
                    }

                    Here at home,

                    malloc(0) returned 902a0

                    vp is non-null and so I assume that malloc did what I asked and
                    allocated 0 bytes successfully. Do you think I should free(vp) to avoid
                    a leak?

                    --
                    Joe Wright
                    "Everything should be made as simple as possible, but not simpler."
                    --- Albert Einstein ---

                    Comment

                    • Eric Sosman

                      #11
                      Re: malloc (0)

                      Joe Wright wrote:
                      Bhaskar wrote:
                      >
                      >Hey can any one tell me what malloc 0 does, it assigns space in memory
                      >i know that but is it usable and how many bytes does it allocate ,
                      >allocation of 0 bytes is difficult-to-digest concept and still in the
                      >memory.
                      >>
                      Try this.
                      >
                      #include <stdio.h>
                      #include <stdlib.h>
                      int main(void) {
                      void *vp = malloc(0);
                      printf("malloc( 0) returned %p\n", vp);
                      return 0;
                      }
                      >
                      Here at home,
                      >
                      malloc(0) returned 902a0
                      >
                      vp is non-null and so I assume that malloc did what I asked and
                      allocated 0 bytes successfully. Do you think I should free(vp) to avoid
                      a leak?
                      Not in this program, but in general yes! Otherwise, you
                      may well leak some extra "bookkeepin g" memory that malloc()
                      uses to keep track of each zero-length allocation. Try

                      #include <stdio.h>
                      #include <stdlib.h>
                      int main(void) {
                      unsigned long count = 0;
                      while (malloc(0) != NULL) {
                      if (++count == 0) {
                      printf ("More than %lu successful allocations"
                      " -- giving up.\n", (unsigned long)-1);
                      return 0;
                      }
                      }
                      printf ("%lu successful allocations before NULL\n",
                      count);
                      return 0;
                      }

                      "Here at home," I get

                      72960776 successful allocations before NULL

                      .... plus a lot of disk activity and some system alerts about
                      increasing the size of the page file. Strongly suggestive of
                      leakage, I'd say.

                      --
                      Eric Sosman
                      esosman@acm-dot-invalid.org

                      Comment

                      • Joe Wright

                        #12
                        Re: malloc (0)

                        Eric Sosman wrote:
                        Joe Wright wrote:
                        >Bhaskar wrote:
                        >>
                        >>Hey can any one tell me what malloc 0 does, it assigns space in memory
                        >>i know that but is it usable and how many bytes does it allocate ,
                        >>allocation of 0 bytes is difficult-to-digest concept and still in the
                        >>memory.
                        >>>
                        >Try this.
                        >>
                        >#include <stdio.h>
                        >#include <stdlib.h>
                        >int main(void) {
                        > void *vp = malloc(0);
                        > printf("malloc( 0) returned %p\n", vp);
                        > return 0;
                        >}
                        >>
                        >Here at home,
                        >>
                        >malloc(0) returned 902a0
                        >>
                        >vp is non-null and so I assume that malloc did what I asked and
                        >allocated 0 bytes successfully. Do you think I should free(vp) to
                        >avoid a leak?
                        >
                        Not in this program, but in general yes! Otherwise, you
                        may well leak some extra "bookkeepin g" memory that malloc()
                        uses to keep track of each zero-length allocation. Try
                        >
                        #include <stdio.h>
                        #include <stdlib.h>
                        int main(void) {
                        unsigned long count = 0;
                        while (malloc(0) != NULL) {
                        if (++count == 0) {
                        printf ("More than %lu successful allocations"
                        " -- giving up.\n", (unsigned long)-1);
                        return 0;
                        }
                        }
                        printf ("%lu successful allocations before NULL\n",
                        count);
                        return 0;
                        }
                        >
                        "Here at home," I get
                        >
                        72960776 successful allocations before NULL
                        >
                        ... plus a lot of disk activity and some system alerts about
                        increasing the size of the page file. Strongly suggestive of
                        leakage, I'd say.
                        >
                        Leaks like a sieve. 73 million useless calls to malloc(0) and you
                        couldn't free() them if you wanted to. Crash and Burn.

                        --
                        Joe Wright
                        "Everything should be made as simple as possible, but not simpler."
                        --- Albert Einstein ---

                        Comment

                        • Mark McIntyre

                          #13
                          Re: malloc (0)

                          On 4 Jul 2006 12:20:41 -0700, in comp.lang.c , "Harald van D?k"
                          <truedfx@gmail. comwrote:
                          >Lew Pitcher wrote:
                          >The key phrase is "implementa tion defined".
                          ....
                          >>As I read it, so long as your C compiler/environment's
                          >implementati on documents it, malloc(0) could legitimately launch
                          >Russian ICBM missles
                          >Unless either returning a null pointer by itself in a user function, or
                          >calling malloc with a nonzero size, is allowed to launch missiles, no,
                          >malloc(0) isn't allowed to launch missiles either.
                          Actually it is, but the implementation must define this behaviour. The
                          DS9K's C compiler provides a very similar feature.
                          --
                          Mark McIntyre

                          "Debugging is twice as hard as writing the code in the first place.
                          Therefore, if you write the code as cleverly as possible, you are,
                          by definition, not smart enough to debug it."
                          --Brian Kernighan

                          Comment

                          • Skarmander

                            #14
                            Re: malloc (0)

                            Mark McIntyre wrote:
                            On 4 Jul 2006 12:20:41 -0700, in comp.lang.c , "Harald van D?k"
                            <truedfx@gmail. comwrote:
                            >
                            >Lew Pitcher wrote:
                            >
                            >>The key phrase is "implementa tion defined".
                            ...
                            >>As I read it, so long as your C compiler/environment's
                            >>implementatio n documents it, malloc(0) could legitimately launch
                            >>Russian ICBM missles
                            >
                            >Unless either returning a null pointer by itself in a user function, or
                            >calling malloc with a nonzero size, is allowed to launch missiles, no,
                            >malloc(0) isn't allowed to launch missiles either.
                            >
                            Actually it is, but the implementation must define this behaviour. The
                            DS9K's C compiler provides a very similar feature.
                            Although it hardly matters in this less than serious context, the DS9K
                            compiler would not be conforming for doing so. (Which would be odd to say
                            the least, since the DS9K manufacturer is usually very careful about this
                            sort of thing.)

                            N1124:
                            "3.4.1 implementation-defined behavior
                            unspecified behavior where each implementation documents how the choice is made"

                            "3.4.4 unspecified behavior
                            use of an unspecified value, or other behavior where this International
                            Standard provides two or more possibilities and imposes no further
                            requirements on which is chosen in any instance"

                            Even the most liberal interpretation of these statements requires that, if
                            the standard gives a choice between behaviors and states the actual behavior
                            is "implementa tion-defined", then the choice must be made from the behaviors
                            given and the choice itself must be documented. The implementation is not
                            allowed to implement additional or replacement behavior, not even if it
                            documents it.

                            The alternative would make "unspecifie d behavior" a synonym for "undefined
                            behavior", which is clearly not the intent.

                            S.

                            Comment

                            • Ben Pfaff

                              #15
                              Re: malloc (0)

                              "Kiru Sengal" <kiru.sengal@gm ail.comwrites:
                              Bhaskar wrote:
                              >Hey can any one tell me what malloc 0 does, it assigns space in memory
                              >i know that but is it usable and how many bytes does it allocate ,
                              >allocation of 0 bytes is difficult-to-digest concept and still in the
                              >memory.
                              >
                              I believe it returns NULL.
                              This is one possible implementation.
                              It may also return a unique address that may not be dereferenced.
                              --
                              int main(void){char p[]="ABCDEFGHIJKLM NOPQRSTUVWXYZab cdefghijklmnopq rstuvwxyz.\
                              \n",*q="kl BIcNBFr.NKEzjwC IxNJC";int i=sizeof p/2;char *strchr();int putchar(\
                              );while(*q){i+= strchr(p,*q++)-p;if(i>=(int)si zeof p)i-=sizeof p-1;putchar(p[i]\
                              );}return 0;}

                              Comment

                              Working...