C, C++ (malloc, calloc, free and realloc) different type of Memory allocation/de-allocation functions
malloc()
Allocates memory and returns a pointer to the first byte of allocated space.The general syntax of malloc() is
ptr =(cast-type*)malloc(byte-size);
Example:
arr=(int*)malloc(10*sizeof(int));
calloc()
Allocates space for an array of elements, initializes them to zero and returns a pointer to the memory.The syntax of calloc() can be given as:
ptr=(cast-type*) calloc(n,elem-size);
free()
Frees previously allocated memory.The general syntax of the free()function is,
free(ptr);
realloc()
Alters the size of previously allocated memory.The general syntax for realloc() can be given as,
ptr = realloc(ptr,newsize);
Comments
Post a Comment