malloc and realloc

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jayfriend
    New Member
    • Dec 2006
    • 8

    malloc and realloc

    Hi All,

    Could anyone tell me the difference between memory allocation scheme used by malloc and realloc. I would like to know how realloc reasssigns the memory to a pointer variable. In other words, what will happen if the availabe memory is not contiguous?

    What will be the situation with malloc if the available memory is not contiguous?

    Finally, is the memory allocated by calloc is contiguous always?


    Please answer to this.

    Thanks in advance.
  • vpawizard
    New Member
    • Nov 2006
    • 66

    #2
    calloc() allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. The memory is set to zero.

    malloc() allocates size bytes and returns a pointer to the allocated memory. The memory is not cleared.

    realloc() changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged to the minimum of the old and new sizes; newly allocated memory will be uninitialized. If ptr is NULL, the call is equivalent to malloc(size); if size is equal to zero, the call is equivalent to free(ptr). Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc().

    For calloc() and malloc(), the value returned is a pointer to the allocated memory, which is suitably aligned for any kind of variable.

    Comment

    Working...