14.18 – Dynamic memory allocation – Part 2

by subbu on February 2, 2014

In the previous session, we have discoursed how malloc() and free() can be used to allocate and de-allocate the memory during runtime. Now, we see how allocated memory can be expanded, compressed and blocks of memory can be allocated.

realloc()

It is the function defined within the header file “stdio.h”. It is used to expand or compress the memory allocation allocated by malloc(). It accepts the address of existed memory allocation and the size of extending memory as arguments.
It allocates the new memory, copies the contents from existed memory to new memory and de-allocated the old memory.
It returns the address of newly allocated memory as void pointer.

realloc() in C language

short *p,*q;
p=(short*)malloc(2);

allocates two bytes of memory, the address of which assigns to the pointer p

*p=540;

allocated memory is assigning with 540

q=(int*)realloc(p,4);

allocates 6 bytes of memory that is 2 bytes to store the data belongs to p and 4 bytes of extending memory

realloc() in action

  1. Allocates the new memory
  2. Copy the contents of existed memory into new memory
  3. De-allocates the old memory

Specification: Accept n elements from the user as the first step and accept m more elements from the user as the second step. Finally print the total elements (n+m).

Program:

#include<stdio.h>
int main()
{
short *p,*q;
short i,n,m;
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
p=(short*)malloc(sizeof(short)*n);       /* allocated "n" elements */
for(i=0;i<n;i++)
  scanf("%d",p+i);
printf("How many more?");
scanf("%d",&m);
q=(short*)realloc(p,sizeof(short)*m);   /* extended to "n+m" elements */
for(i=n;i<n+m;i++)
  scanf("%d",q+i);
printf("Total elements:\n");
for(i=0;i<n+m;i++)
  printf("%5d",*(q+i));
free(q);
return 0;
}

Execution:
How many elements?3
Enter 3 elements:
12   34   56
How many more?5
Enter 5 elements:
22   33   44   56   77
Total elements:
12   34   56   22   33   44   56   77

Example explained:
Allocated the memory to store n elements
Extended the memory to store m more elements
Finally printed n+m elements

calloc():

It is the function defined in “stdio.h”. It is used to allocate blocks of memory. It accepts two arguments that are the number of blocks and the size of each block. It returns the address of allocated memory as void pointer.

calloc() in C language

Specification: Accept nxm matrix and print the same using dynamic memory allocation

#include<stdio.h>
int main()
{
short *p,*q,i,j,n,m;
printf("Enter the class of matrix:\n");
scanf("%d%d",&n,&m);
p=(short*)calloc(n,sizeof(short)*m); /* allocated n rows of mx2 bytes */
printf("Enter %dx%d matrix:\n",n,m);
for(q=p,i=0;i<n;i++)
  for(j=0;j<m;j++,q++)
    scanf("%d",q);
printf("The given matrix:\n");
for(q=p,i=0;i<n;i++)
{
  for(j=0;j<m;j++,q++)
    printf("%5d",*q);
  printf("\n");
}
free(p);
return 0;
}

Execution:
Enter the class of matrix:
3   3
Enter 2×3 matrix:
3    4    5
6    7    8
1    9    0
The given matrix:
3    4    5
6    7    8
1    9    0

Example explained:
Allocated n blocks of mx2 bytes
Address of memory is stored in a pointer p
q is used to traverse every element using pointer arithmetic.

calloc() in Action

Previous post:

Next post: