Can anyone plz tell what is the difference between malloc and calloc?
Can anyone plz tell what is the difference between malloc and calloc?
Google, use the man pages, check wikipedia..... Summed up:
Similarities:
- Allocate memory dynamically.
- Return NULL if contiguous block of memory matching the size requested could not be allocated.
Differences:
- malloc() just takes the size in bytes as a size_t argument, while calloc() wants two arguments, one being the size of the element this memory is being requested for, and the other being the amount of elements that should be able to fit in this block of memory (ie. an element could be an int, and you want to fit 4 ints into this block of memory).
- If calloc() succeeds, it sets the value for each byte of allocated memory to 0. malloc() is not guarenteed to do such, but some compilers might do this, notably I think Visual C++ in debug mode does this. Never assume such for malloc().
This was asked already, so you might want to search the last page or two of this section of the forums.