Monday, February 11, 2013

Malloc, calloc, and Realloc

                              Calloc, malloc, free, realloc
                               -------------------------------------------
void *calloc(size_t nmemb, size_t size);
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. if nmemb or size is 0, then calloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().
** the memory is initialized to zeros.

void *malloc(size_t size);
allocates size bytes and returns a pointer to the allocated memory (BLOCK). The memory is neot cleared. If size is 0, then malloc() returns either NULL or a unique pointer value that can later be successfully passed to free().
** The memory is uninitialized.

Ex1:
int *ptr;
ptr=(int*) malloc(sizeof(int));

Ex2.
struct employee *ptr;
ptr=(struct employee *ptr)malloc (sizeof(struct employee));

void free( void *ptr);
free() frees the memory space pointed to by ptr, which must have been returned by a prevoious call to malloc(), caloc() or realloc(). otherwise, or if free(ptr) has already been called before, undefined behaviour occurs. If ptr is NULL, no operation is performed.

void *realloc ( void *ptr, size_t size)
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 alllocated memory will be uninitialized. If ptr is NULL, then the call is equivalent to malloc(size), for all values of size; if size is equal to sero, and pt is not NULL, then the call is equivalent ot free(ptr). Unless ptr is NULL, it must have been returned by an earlier call to malloc(), calloc or realloc(). If the area pointed to was moved, a freeptr) is done.
------------------------------------------------------------------------------------------------------------------------

No comments:

Post a Comment

Qualcomm Short Term

  113 is the SL. 1st Target by mid July.

Total Pageviews