In C, dynamic memory allocation is a very important area, which you should know. Let us start with an example.
If you want to work with an array. You have to specify the size of the array at the time of declaration. Now, some users may work with a large number of data and some users may work with very small number of data. So, as a developer, you should declare the array with such a size, which can be used by every user.
Here the problem is, if the user works with a very small number of data, huge volume of memory space is not in use. And using an array, memory is being allocated based on its size specified at the time of declaration. But if this memory can be allocated at run time, it will be beneficial.
Hence, the chunk of memory can be allocated at run time using dynamic memory allocation.
In C programming language, three functions (malloc, calloc, and realloc) are used for dynamic memory allocation and one function (free) is used for deallocation.
malloc takes one argument which should be an unsigned integer value telling the function how many bytes of storage to allocate. It returns a pointer to the first memory location in that storage. This function does not initialize the newly allocated memory.
calloc takes two arguments, where the first argument should be an unsigned integer value telling the function about the number of elements for which memory will be allocated and the second argument is the size of an element. This returns a pointer to the first memory location in that storage. This function initializes the newly allocated memory by default value.
realloc is a function that attempts to change the size of a previously allocated block of memory. The new size can be larger or smaller. If the block is made larger then the old contents remain unchanged and memory is added to the end of the block. If the size is made smaller then the remaining contents are unchanged.
free deallocates memory from a pointer.
Example using malloc
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, n, *ptr, sum = 0;
// take number of elements as input
printf("How many elements do you want to store?");
scanf("%d", &n);
ptr = (int *) malloc(n * sizeof(int));
// test default values of ptr
for(i = 0; i < n; i++)
printf(" %d ", ptr[i]);
// take the elements as input
printf("Enter the elements:");
for(i = 0; i < n; i++)
scanf("%d", &ptr[i]);
// calculate sum
for(i = 0; i < n; i++)
sum += ptr[i];
printf("\nSum is %d", sum);
return 0;
}
Example using calloc
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, n, *ptr, sum = 0;
// take number of elements as input
printf("How many elements do you want to store?");
scanf("%d", &n);
ptr = (int *) calloc(n, sizeof(int));
// test default values of ptr
for(i = 0; i < n; i++)
printf(" %d ", ptr[i]);
// take the elements as input
printfamp;("Enter the elements:");
for(i = 0; i < n; i++)
scanf("%d", &ptr[i]);
// calculate sum
for(i < 0; i < n; i++)
sum += ptr[i];
printf("\nSum is %d", sum);
return <;
}
Example using realloc
The following program illustrates how realloc function is used to change memory allocation at run-time.
In this program, first a memory block is allocated for variable str. Later we need to change size of the memory block, thus we have to call realloc function to increase size of the memory block. This function performs reallocation without affecting previous data.
In this function, we are also using free function to deallocate the memory which is pointed by variable str.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, j, n, *ptr, sum = 0;
// take number of elements as i<put
printf("How many elements do yo< want to store? ");
scanf("%d", &n);
ptr = (int *) calloc(n, sizeof(int));
// test default values of ptr
for(i = 0; i < n; i++)
printf(" %d ", ptr[i]);
printf("\n");
amp;
// take the elements as input
printf("Enter the elements:");
for(i = 0; i < n; i++)
scanf("%d", &ptr[i]);
// calculate sum
for(i = 0; i < n; i++)
sum += ptr[i];
printf("\nSum is %d", sum);
// take number of elements as input
printf("\nIf you want to store more elements? \nEnter number of elements you want to store. ");
scanf("%d", &n);
ptr = (int *) realloc (ptr, n);
// take the elements as input
printf("Enter additional elements:");
for(j = i; j < n; j++)
scanf("%d", &ptr[j]);
// calculate sum
for(j = i; j < n; j++)
sum += ptr[j];
printf("\nSum is %d", sum);
free(ptr);
return 0;
}
Dynamic memory allocation for 2D array
#include <stdio.h>
int main()
{
int i, j, m, n;
int **arr;
int *row;
printf("Enter the order of matrix 1: ");
scanf("%d %d", &m, &n);
arr = (int **) malloc (m * sizeof(int *));
for(i = 0; i < m; i++)
{
row = (int *) malloc (n * sizeof(int));
arr[i] = row;
}
printf("Enter the values of matrix 1: \n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf("%d", &arr[i][j]);
}
}
printf("The given matrix is: \n");
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf(" %d", arr[i][j]);
}
printf("\n");
}
return 0;
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.