Dynamic Memory Allocation can be defined as a procedure in which the size of a data structure (like Array) is changed during the runtime.
C provides some
functions to achieve these tasks. There are 4 library functions provided by C
defined under <stdlib.h> header file to facilitate
dynamic memory allocation in C programming. They are:
- malloc()
- calloc()
- free()
- realloc()
1. malloc() method
“malloc” or “memory allocation” method
in C is used to dynamically allocate a single large block of memory with the
specified size.
It returns a pointer of
type void which can be cast into a pointer of any form. It initializes each
block with default garbage value.
Syntax:
ptr = (cast-type*)
malloc(byte-size)
ptr =
(int*) malloc(100 * sizeof(int));
Since the
size of int is 2 bytes, this statement will allocate 200 bytes of memory. And,
the pointer ptr holds the address of the first byte in the allocated memory.
2. calloc() method
“calloc” or “contiguous allocation” method
in C is used to dynamically allocate the specified number of blocks of memory
of the specified type. It initializes each block with a default value ‘0’.
Syntax:
ptr =
(cast-type*)calloc(n, element-size);
ptr =
(float*) calloc(25, sizeof(float));
This
statement allocates contiguous space in memory for 25 elements each with the
size of the float.
3. free()
method
“free” method in C is used to dynamically de-allocate the
memory. The memory allocated using functions malloc() and calloc() is not
de-allocated on their own.
Hence the free() method
is used, whenever the dynamic memory allocation takes place. It helps to reduce
wastage of memory by freeing it.
Syntax:
free(ptr);
4. realloc() method
“realloc” or “re-allocation” method
in C is used to dynamically change the memory allocation of a previously allocated
memory.
If the memory
previously allocated with the help of malloc or calloc is insufficient, realloc
can be used to dynamically re-allocate memory.
re-allocation of memory
maintains the already present value and new blocks will be initialized with
default garbage value.
Syntax:
ptr = realloc(ptr,
newSize);
where ptr is
reallocated with new size 'newSize'.
Example 1:
// Program to calculate
the sum of n numbers entered by the user
#include
<stdlib.h>
int main()
{
int n, i, *ptr, sum = 0;
printf("Enter number of elements:
");
scanf("%d", &n);
ptr = (int*) malloc(n * sizeof(int));
// if memory cannot be allocated
if(ptr == NULL)
{
printf("Error! memory not
allocated.");
exit(0);
}
printf("Enter elements: ");
for(i = 0; i < n; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
// deallocating the memory
free(ptr);
return 0;
}
Example 2:
/ Program to calculate
the sum of n numbers entered by the user
#include
<stdio.h>
#include
<stdlib.h>
int main()
{
int n, i, *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);
}
for(i = 0; i < n; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}
printf("Sum = %d", sum);
free(ptr);
return 0;
}
Example 3:
realloc()
#include
<stdio.h>
#include
<stdlib.h>
int main()
{
int *ptr, i , n1, n2;
printf("Enter size: ");
scanf("%d", &n1);
ptr = (int*) malloc(n1 * sizeof(int));
printf("Addresses of previously
allocated memory: ");
for(i = 0; i < n1; ++i)
printf("%u\n",ptr + i);
printf("\nEnter the new size: ");
scanf("%d", &n2);
// rellocating the memory
ptr =
realloc(ptr, n2 * sizeof(int));
printf("Addresses of newly allocated
memory: ");
for(i = 0; i < n2; ++i)
printf("%u\n", ptr + i);
free(ptr);
return 0;
}
No comments:
Post a Comment