Thursday, May 26, 2011

notes on calloc and malloc

calloc() allocates memory for an array of nmemb elements of sizebytes 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.

free() frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(),


calloc() orrealloc(). Otherwise, or if free(ptr)has already been called before, undefined behaviour occurs. If ptr is NULL, no operation is performed.

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). Unlessptr is NULL, it must have been returned by an earlier call to malloc(), calloc() or realloc(). If the area pointed to was moved, a free(ptr) is done.

No comments: