malloc

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Parul Bagadia
    New Member
    • Mar 2008
    • 188

    malloc

    Is there some logic behind the syntax of malloc function?
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Um, I'm not entirely sure what you're asking here, but the answer is yes. malloc() allows you enter in the number of bytes you need allocated as a parameter so that you can get the amount you need. It returns a void* so that you can cast it to whatever type of pointer you need. I think you may be asking about the logic of casting syntax, which I think of as a sort of "multiplication ". For example,
    [CODE=c]char* c;
    int* i = (int*)c;
    [/CODE]
    says to me "take this pointer and 'multiply' by the new type", thus changing the way pointer arithmetic works with it and how the compiler interprets the data it points to.

    Comment

    • oler1s
      Recognized Expert Contributor
      • Aug 2007
      • 671

      #3
      Parul, have you ummm, considered checking documentation?

      Comment

      • Parul Bagadia
        New Member
        • Mar 2008
        • 188

        #4
        Originally posted by oler1s
        Parul, have you ummm, considered checking documentation?
        What is it? M not so sure that i got you.

        Comment

        • Parul Bagadia
          New Member
          • Mar 2008
          • 188

          #5
          first=(*struct linklist)malloc (sizeof(struct linklist));
          Here the * mark should have been at the end;
          first =(struct linklist*)mallo c(sizeof(struct linklist));
          but when i wrote the earlier statement; my compiler showed the error that linklist unexpected.
          linklist is the name of my struct.
          Does somebody know why do we use * at the end only?
          And is it used for symbolic representation of multiplication? ; as i got it from some earlier replies?
          Is that the logic behind using *? or is it something else?

          Comment

          • gpraghuram
            Recognized Expert Top Contributor
            • Mar 2007
            • 1275

            #6
            Originally posted by Parul Bagadia
            first=(*struct linklist)malloc (sizeof(struct linklist));
            Here the * mark should have been at the end;
            first =(struct linklist*)mallo c(sizeof(struct linklist));
            but when i wrote the earlier statement; my compiler showed the error that linklist unexpected.
            linklist is the name of my struct.
            Does somebody know why do we use * at the end only?
            And is it used for symbolic representation of multiplication? ; as i got it from some earlier replies?
            Is that the logic behind using *? or is it something else?


            I think you need to check the pointer basics.
            * at the beginning means the value hold by the pointer and * at the end dosent mean multiplication, but u are saying it is of that pointer type.

            Raghuram

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              The * is overloaded in C.

              Sometimes it means de-reference. Sometimes itr means pointer and sometimes it means multiplication.

              Each meaning is exclsive from the rest. Therefore, the exact meaning of the * is by the context in which it is used.

              As to your malloc() question:
              1) malloc() allocates a block of memory in bytes.
              2) the number of bytes to allocate is the malloc() argument
              3) and * used in the argument is multiplication.
              4) malloc returns the address of the allocation as a void*
              5) the * in void* means pointer.
              6) you can cast this pointer to your own pointer.
              7) you will use an * again for the cast to mean pointer.

              Comment

              • Parul Bagadia
                New Member
                • Mar 2008
                • 188

                #8
                Originally posted by weaknessforcats
                The * is overloaded in C.

                Sometimes it means de-reference. Sometimes itr means pointer and sometimes it means multiplication.

                Each meaning is exclsive from the rest. Therefore, the exact meaning of the * is by the context in which it is used.

                As to your malloc() question:
                1) malloc() allocates a block of memory in bytes.
                2) the number of bytes to allocate is the malloc() argument
                3) and * used in the argument is multiplication.
                4) malloc returns the address of the allocation as a void*
                5) the * in void* means pointer.
                6) you can cast this pointer to your own pointer.
                7) you will use an * again for the cast to mean pointer.
                Thanx for explaining so nicely.
                But according to your first point where is it used as de-reference?
                As in m asking for example.
                And what exactly is de-refernce? Is it to free some reference or what?

                Comment

                • Laharl
                  Recognized Expert Contributor
                  • Sep 2007
                  • 849

                  #9
                  Parul: Dereferencing is anytime you access (or assign) the data a pointer points to using *. For example:[CODE=c]
                  int* x = (int*)(malloc(s izeof(int));
                  *x = 4;
                  [/CODE]
                  Thus, a 4 is now held in the memory whose address is at x.

                  dhranidar: Please make your own thread rather than hijacking this one. Thank you.

                  Comment

                  • Parul Bagadia
                    New Member
                    • Mar 2008
                    • 188

                    #10
                    Originally posted by Laharl
                    Parul: Dereferencing is anytime you access (or assign) the data a pointer points to using *. For example:[CODE=c]
                    int* x = (int*)(malloc(s izeof(int));
                    *x = 4;
                    [/CODE]
                    Thus, a 4 is now held in the memory whose address is at x.

                    dhranidar: Please make your own thread rather than hijacking this one. Thank you.
                    I didnt get the first word of your last line.

                    Comment

                    • Parul Bagadia
                      New Member
                      • Mar 2008
                      • 188

                      #11
                      How the sizeof operatof used in malloc makes malloc machine independant?
                      m not sure about what it makes machine independent.

                      Comment

                      • Laharl
                        Recognized Expert Contributor
                        • Sep 2007
                        • 849

                        #12
                        I was addressing the last sentence to the other user, Parul. As to how sizeof makes malloc or any other code more platform independent, on a 32bit machine, sizeof(int) is 4. This covers nearly all computers released in the last 20 years. However, recently, 64bit machines have begun to come out, on which sizeof(int) would be 8. Hardcoding the 4 on one of those machines would lead to unexpected outcomes, like thinking arrays are twice as long as they actually are.

                        Comment

                        • Banfa
                          Recognized Expert Expert
                          • Feb 2006
                          • 9067

                          #13
                          Originally posted by Laharl
                          As to how sizeof makes malloc or any other code more platform independent, on a 32bit machine, sizeof(int) is 4. This covers nearly all computers released in the last 20 years
                          There are still plenty of 8 and 16 bit micro-processors in production today with C compilers whose sizeof(int) is often 2.

                          See, PIC, Rabit or AVR micro-processors for an example.

                          Comment

                          • Laharl
                            Recognized Expert Contributor
                            • Sep 2007
                            • 849

                            #14
                            True, true, I was thinking in terms of PCs, but you raise a very good point.

                            Comment

                            Working...