Showing posts with label Dynamic memory allocation. Show all posts
Showing posts with label Dynamic memory allocation. Show all posts

Friday 30 March 2012

Dynamic memory allocation


Dynamic memory allocation

The process of allocation memory at run time is called dynamic memory allocation. Its advantages are:
1.                 When programmer actually need to use memory space at that time dynamically allocates so save memory space.
2.                 Programmer doesn’t need to know in advance how many variables are needed.
There are mainly four types function available for dynamic memory allocation.

malloc()

It allocates request size of bytes and returns a pointer to the first byte of the allocated space.
Syntax
ptr-var=(cast type) malloc(byte size);
Here, ptr-var is a pointer of type cast-type. It returns a pointer to an area of the memory with size byte-size.
Ex,
          int *x;
          x=(int *)malloc(10*sizeof(int));

In above example , a memory space 100*size of int(200) bytes is reserved and the address of the first byte of the memory allocated is assigned to the pointer a of type of int.

calloc()

This function allocates space for an array of elements. This function is normally used for derived data type such as array and structure.

malloc() allocates single block of storage space and calloc() allocates multiple block of storage.
Syntax:
ptr=(cast-type)calloc(n,element-size);
Here, ptr is a pointer of type cast. The first argument n is number of variable for what need to allocate space. The second argument element-size is  the size of each element in byte.

If allocation  of memory space is successful  , all bytes are initialized to zero and a pointer to the first byte of the allocated region is returned. If there is no enough space NULL pointer is returned.

Ex,
struct emp
{
   int no;
   char *name;
   float age;
}
-----------
-----------
struct emp *e;
e=(struct emp *)calloc(10,sizeof(struct emp));

Here, array of 10 structure object is create.

Program  : Create array of structure dynamically.
#include<stdio.h>
#include<conio.h>
#include<string.h>

struct stud
{
          int sno;
          char sname[15];
          int m1,m2,m3,total;
};

void main()
{
          struct stud *s;
          int i,n;
          clrscr();

          printf("\n Enter total number of student : ");
          scanf("%d",&n);

          s=(struct stud *)calloc(n,sizeof(struct stud));

          for(i=0;i<n;i++)
          {
                   printf("\n ----- student = %d ",i+1);

                   printf("\n Enter No :");
                   scanf("%d",&s->sno);
                   fflush(stdin);
                   printf("\n Enter Name:");
                   gets(s->sname);
                   printf("\n Enter M1 M2 M3:");
                   scanf("%d %d %d",&s->m1,&s->m2,&s->m3);
                   //calculation
                   s->total=s->m1 + s->m2 +s->m3;
                   s++;

            }
          clrscr();
          s=s-n;
          printf("\n output");
          printf("\n _______________________________________________________");
          printf("\n \t\t\t Mark sheet ");
          printf("\n _______________________________________________________");

          for(i=0;i<n;i++)
          {
                   printf("\n address = %u",s);
                   printf("\n\n No:%d",s->sno);
                   printf("\n Name :%s",s->sname);
                   printf("\n\n  m1  \t  m2  \t  m3  \t  total");
                   printf("\n%4d\t%4d\t%4d\t%5d",s->m1,s->m2,s->m3,s->total);
                   printf("\n_____________________________________________________");
                   s++;
          }
          free(s);
          getch();
}


realloc()

When need to change the size of previously allocated memory space then realloc() is used. It is known as reallocation of memory.
Syntax:
ptr=realloc(ptr,newsize)

Here, ptr is the pointer to the original block of memory. The newsize specifies the size in byte , the function returns pointer to the first byte of new memory block.The newsize may be larger or smaller than the size.

If the function is unsuccessful in locating additional space , it returns as a NULL pointer and original block is lost.
Ex,
ptr=(int *)malloc(20*sizeof(int));
-------
-------
ptr=(char *)realloc(30*sizeof(int));

Program : Use of realloc()

#include<stdio.h>
#include<conio.h>

void main()
{
          int *a,i,n,sum=0;
          clrscr();

          printf("\n\n Enter how many number : ");
          scanf("%d",&n);
          a=(int *)malloc(n*sizeof(int));

          for(i=0;i<n;i++)
           {
                   printf("\n Enter The Value Of a[%d] : ",i);
                   scanf("%d",&a[i]);

           }


          for(i=0;i<n;i++)
           {
                   printf("\n a[%d] : %d",i,a[i] );
                   sum=sum+a[i];

          }
          printf("\n Sum = %d",sum);
          printf("\n\n Enter new number to reallocate : ");
          scanf("%d",&n);
          a=(int *)realloc(a,n);

          for(i=0;i<n;i++)
           {
                   printf("\n Enter The Value Of a[%d] : ",i);
                   scanf("%d",&a[i]);

           }


          for(i=0;i<n;i++)
           {
                   printf("\n a[%d] : %d",i,a[i] );
                   sum=sum+a[i];

          }
          printf("\n sum after reallocation : %d",sum);
          getch();
}


free()

The release of storage space becomes important when the storage is limited.free() function is used to release(deallocate)  that block of memory which is created by malloc() and calloc().
Sytax:

free(ptr);
Here, the ptr is the pointer variable which previously allocated dynamic memory.
Ex,
ptr=(int *)malloc(20*sizeof(int));
free(ptr);


Posted By : Ruchita Pandya