Dynamic Memory Allocation
Sample Codes
malloc Example
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c;
int i;
int *p;
printf("Enter number of elements need to create in Array:");
scanf("%d", &c);
p = (int*)malloc(c*sizeof(int));
if(p == NULL)
{
printf("memory cannot be allocated\n");
}
else
{
printf("Enter the values to elements of array:\n");
for(int i=0;i<c;i++)
{
printf("Enter value for element %d: ", i+1);
scanf("%d", (p+i));
printf("Element %d, save to this Memory Address: %p\n", i , (void*)(p+i));
}
printf("\n");
printf("\n");
printf("Print elements of Array:\n");
for(i=0;i<c;i++)
{
printf("Element %d value: %d\n", i+1, *(p+i));
printf("Element %d, value read from this Memory Address: %p\n", i , (void*)(p+i));
}
free(p);
printf("\n");
printf("\n");
printf("calling after free\n", (void*)p);
for(i=0;i<c;i++)
{
printf("Element %d value: %d\n", i+1, *(p+i));
}
}
return 0;
}
Enter number of elements need to create in Array:3
Enter the values to elements of array:
Enter value for element 1: 10
Element 0, save to this Memory Address: 00000000000B6EA0
Enter value for element 2: 20
Element 1, save to this Memory Address: 00000000000B6EA4
Enter value for element 3: 30
Element 2, save to this Memory Address: 00000000000B6EA8
Print elements of Array:
Element 1 value: 10
Element 0, value read from this Memory Address: 00000000000B6EA0
Element 2 value: 20
Element 1, value read from this Memory Address: 00000000000B6EA4
Element 3 value: 30
Element 2, value read from this Memory Address: 00000000000B6EA8
calling after free
Element 1 value: 726016
Element 2 value: 0
Element 3 value: 721232
calloc Example
#include <stdio.h>
#include <stdlib.h>
int main()
{
int c;
int i;
int *p;
int sum;
sum =0;
printf("Enter number of elements need to create in Array:");
scanf("%d", &c);
p = (int*)calloc(c, sizeof(int));
if(p == NULL)
{
printf("memory cannot be allocated\n");
}
else
{
printf("Print the total sum of array (before assigning the values):\n");
for(int i=0;i<c;i++)
{
sum += *(p+i);
}
printf("sum = %d", sum);
printf("\n");
printf("\n");
printf("Enter the values to elements of array:\n");
for(int i=0;i<c;i++)
{
printf("Enter value for element %d: ", i+1);
scanf("%d", (p+i));
printf("Element %d, save to this Memory Address: %p\n", i , (void*)(p+i));
sum += *(p+i);
}
printf("sum = %d", sum);
printf("\n");
printf("\n");
free(p);
printf("Print the total sum of array (after calling free(p)):\n");
for(int i=0;i<c;i++)
{
sum += *(p+i);
}
printf("sum = %d", sum);
}
return 0;
}
Enter number of elements need to create in Array:5
Print the total sum of array (before assigning the values):
sum = 0
Enter the values to elements of array:
Enter value for element 1: 10
Element 0, save to this Memory Address: 00000000009A6EA0
Enter value for element 2: 20
Element 1, save to this Memory Address: 00000000009A6EA4
Enter value for element 3: 30
Element 2, save to this Memory Address: 00000000009A6EA8
Enter value for element 4: 40
Element 3, save to this Memory Address: 00000000009A6EAC
Enter value for element 5: 50
Element 4, save to this Memory Address: 00000000009A6EB0
sum = 150
Print the total sum of array (after calling free(p)):
sum = 20190744