#include<stdio.h>
void main()
{
int n, i,*p;
printf(“enter no. of input values:”);
scanf(“%d”, &n);
p=(int*)malloc(sizeof(int)*n);
if(p==NULL)
{
printf(“\n not enough memory”);
return;
}
for(i=0;i<n; i++)
{
printf(“enter any value:”);
scanf(“%d”, &p[i]);
}
printf(“values are:”);
for(i=0;i<n; i++)
printf(“%d”, p[i]);
}
Enter no. of input values: 3
Enter any value: 5
Enter any value: 10
Enter any value: 20
Values are: 5 10 20
In the above, the array created with required size of memory and assessed through pointer. Here the input size(n) can be anything and cannot be known at coding time, thus it makes efficient usage of memory.
The name calloc stands for "contiguous allocation". The only difference between malloc() and calloc() is that, malloc() allocates single block of memory whereas calloc() allocates multiple blocks of memory each of same size and sets all bytes to zero.
The calloc function allocates space for an array of items and it is also like malloc(), but after allocation of memory, it clears the garbage values by filling zeros in all bytes in the memory.
Syntax : pointer=calloc(int num, int size);
It takes two arguments, first one is number of items to be allocated and second one is size of the item.
Example : float *p;
p=(float*)calloc(5,sizeof(float)); //allocating memory for 5 float numbers
#include <stdio.h>
#include <stdlib.h>
int main(){
int n, ,*ptr, sum=0;
printf(“Enter number of elements : “);
scanf("%d", &n);
ptr=(int*)calloc(n, sizeof(int));
if(ptr==NULL)
{
printf(“Error! memory not allocated.”);
exit(0);
}
printf(“Enter elements of array : “);
for(i=0;i<n;++i)
{
scanf(“%d”, ptr+i);
sum+=*(ptr+ i);
}
printf(“Sum = %d”, sum);
free(ptr);
return 0;
}
Enter number of elements : 3
Enter elements of array : 10 20 30
Sum = 60
S.no | malloc() | calloc() |
---|---|---|
1 | It allocates only single block of requested memory | It allocates multiple blocks of requested memory |
2 | int *ptr, ptr = malloc( 20 * sizeof(int) );For the above, 20*4 bytes of memory only allocated in one block. Total = 80 bytes |
int *ptr; Ptr = calloc( 20, 20 * sizeof(int) );For the above, 20 blocks of memory will be created and each contains 20*4 bytes of memory. Total = 1600 bytes |
3 | malloc () doesn’t initializes the allocated memory. It contains garbage values | calloc () initializes the allocated memory to zero |
4 | type cast must be done since this function returns void pointer int *ptr; ptr = (int*)malloc(sizeof(int)*20 ); | Same as malloc () function int *ptr; ptr = (int*)calloc( 20, 20 * sizeof(int) ); |
The name realloc() stands for “reallocation”. If the previously allocated memory is insufficient or more than sufficient. Then, you can change memory size previously allocated using realloc().
It is used to resize (reduce or expand) the memory which is already allocated using “malloc()” or “calloc()” or “realloc()”. It takes base address of previously allocated memory and new size which is currently required.
While expanding the memory size using “realloc()”, if the required amount of memory is not available at the consecutive locations, then allocates memory at another available location, and copies the existing contents to the newly allocated memory area and then releases the old memory. If the memory is not available, then it returns null.
Syntax : ptr=realloc(ptr, new size);
#include <stdio.h>
#include <stdlib.h>
int main(){
int *ptr,i,n1,n2;
printf(“Enter size of array: “);
scanf(“%d”,&n1);
ptr=(int*)malloc(n1*sizeof(int));
printf(“Address of previously allocated memory: “);
for(i=0;i<n1;++i)
printf(“%u\t”, ptr+i);
printf(“\n Enter new size of array: “);
scanf(“%d”,&n2);
ptr=realloc(ptr,n2);
printf(“Address of newly allocated memory:”);
for(i=0;i<n2;++i)
printf(“%u\t”, ptr+i);
return 0;
}
Enter size of array: 3
Address of previously allocated memory: 2303024 2303028 2303032
Enter size of new array: 5
Address of newly allocated memory: 2303024 2303028 2303032 2303036 2303040
It is used to release the memory which is no longer required by the program. This memory must not be static(int a[10]). This must be a dynamic memory which is allocated by malloc() or other functions. It takes the base address of memory as an argument and frees.
Syntax : void free(void *ptr);
If a null pointer is passed as argument, no action occurs. And this function does not return any value.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
char *str;
/* Initial memory allocation */
str = (char *) malloc(20);
strcpy(str, "freetimelearning");
printf("String = %s, Address = %u\n", str, str);
/* Reallocating memory */
str = (char *) realloc(str, 25);
strcat(str, ".com");
printf("String = %s, Address = %u\n", str, str);
/* Deallocate allocated memory */
free(str);
return(0);
}
String=freetimelearning,
Address=19263504
String=freetimelearning.com,
Address=19267648