malloc and calloc reg

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeyshree
    New Member
    • Jul 2010
    • 75

    malloc and calloc reg

    Hai,
    This situation will never arise in computers.But still to understand the difference between malloc and calloc i just visualised the situation.The situation is"consider the RAM in computer which has only bytes 10000 to 10007 and 10028 and 10029 left free.All other portion of memory are used for other purposes."Now what will happen if i give malloc(10) and calloc(5,2)?
    One of my friends said that malloc will not allocate memory as there is no continuous group of 10 bytes.Yet calloc will allocate memory.I have two doubts arising from that?They are
    1)Is that correct?
    2)If so, what will happen if the ram has memory locations 10000 to 10006 and 10027 to 10029 left free,
    all other portion of memory are used for other purposes and i give the command calloc(5,2)?Wil l it allocate memory or not?
    Thanks for your reply in advance.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    malloc allocates in contiguous bytes. That is, each byte is adjacent to the next.

    It must do this or you couldn't do pointer arithmetic on the allocation which would mean you couldn't use malloc for arrays.

    calloc just calls malloc but sets each allocated byte to binary zero.

    calloc(5.2) is senseless. The 5.2 would get converted to an int of 5 which would prompt a compiler warning about potential loss of data.

    It is common for memory allocations to leave bytes here and there in a non-allocatable state.

    Comment

    • jeyshree
      New Member
      • Jul 2010
      • 75

      #3
      hai,
      do you mean calloc too cant allocate memory when there is no continuous group of memory which is left free.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Yes.

        All memory alocators need continuous memory locations. Otherwise you have no idea how to use pointers inside that allocation.

        Always check the return value of malloc and calloc. A return of zero means no allocation possible. A one means continuous bytes were found.

        Comment

        • nextstep
          New Member
          • Aug 2012
          • 14

          #5
          Hi there,

          I think have 2 things of different from malloc and calloc as below:

          1.
          - malloc allocates memory to basic type as int, float...
          - calloc is familiar with malloc but it can allocate memory to user define type as structure type, union type...

          2.
          - malloc allocates a array of bytes of memory
          - calloc allocates a block of size bytes of memory

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Allocations are made in bytes. Not types.

            Look at how an allocation is made. For 100 ints you code
            100 * sizeof(int). For a struct you code
            100 * sizeof(MyStruct ). Every allocation using a type uses sizeof to get the correct number of bytes for that type.

            There is no special formatting for various types. You use a type to determine how to use those bytes. If you say 4 bytes are an int then there is an assumed sign bit, which if you never initialize, will have the same it had when the allocation was made.

            There's no difference between a "block of bytes" and an "array of bytes". That's just two names for the same thing.

            Comment

            • nextstep
              New Member
              • Aug 2012
              • 14

              #7
              Hi weaknessforcats ,

              Thank for your illustration but I didn't said "Allocation s are made in bytes", ok?

              I said:
              "malloc allocates a array of bytes of memory" // actually, I mean that malloc will allocate in contiguous bytes // as when you declare a array

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                True. But I was referring to allocation of bytes for a struct type. There may be no array in this case but the bytes are still contiguous.

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  @nextstep: your first post suggested there is a difference in what a program can do with the memory allocated by malloc or calloc. This is emphatically not the case. Memory allocated by either function can be used to hold any data type(s).

                  The one and only difference between these functions is that the memory provided by malloc is uninitialized and could contain any random value; while the memory provided by calloc is initialized to all zeroes.

                  It is entirely possible that neither function provides a legitimate initial value. For example, suppose you ask these functions to allocate memory for an enumerated type that takes the values 1, 2, and 3. malloc is unlikely to provide one of those values and calloc is guaranteed not to.

                  Comment

                  • nextstep
                    New Member
                    • Aug 2012
                    • 14

                    #10
                    @donbock

                    You are right as manpage (netbsd) explains as below.

                    Code:
                         The malloc() function allocates size bytes of uninitialized memory.  The
                         allocated space is suitably aligned (after possible pointer coercion) for
                         storage of any type of object.
                    
                         The calloc() function allocates space for number objects, each size bytes
                         in length.  The result is identical to calling malloc() with an argument
                         of ``number * size'', with the exception that the allocated memory is
                         explicitly initialized to zero bytes.

                    Comment

                    Working...