Welcome !!

NICETECHVIDYA

Tutorials

Web Designs

You are welcome to Aptech Designers, designed by Pratik from Nepal.

Creativity is my passion. Logo describes your details about company.

Graphics Design needs vision to create yours unique digital concept.

Responsive and All Browser Compatible Dynamic Web Layouts our target.

Home Python C Language J A V A Our Courses
This WebSite is under Construction....

Friday, 11 March 2022

61.pointer:Realloc(), Specification1

 

realloc():

It is the function defined within the header file "stdio.h"

It is used to extend 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.

Example:

 

int *p,*q;

---------------

---------------

---------------

p=(int*)malloc(2);

---------------

---------------

q=(int*)realloc(4);

---------------

---------------

 

 

 

 

 


 

 

 

 

 

 

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

 

Specification1:

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>

void main()

{

  int *p,*q;

  int i,n,m;

  clrscr();

  printf("How many elements?");

  scanf("%d",&n);

  printf("Enter %d elements:\n",n);

  p=(int*)malloc(n*2);       /* allocated "n" elements */

  for(i=0;i<n;i++)

    scanf("%d",p+i);

  printf("How many more?");

  scanf("%d",&m);

  q=(int*)realloc(p,m*2);   /* 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));

  getch();

}

 

 

 

 

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 printing "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.

 

Function definition:

void* calloc(int blocks,int block_size)

{

    --------------------------

    --------------------------

    --------------------------

}

 

Example:

#include<stdio.h>

void main()

{

  int *p,*q,i,j,n,m;

  clrscr();

  printf("Enter the class of matrix:\n");

  scanf("%d%d",&n,&m);

  p=(int*)calloc(n,m*2); /* 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");

  }

  getch();                                                         

}

 

Execution:

Enter the class of matrix:

3   3

Enter 2x3 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 arithmetics.

 

 

 

 

 

 

 

 

 

 

0 comments:

Post a Comment