42. Dynamic Memory Allocation

In this tutorial you will learn about C Programming - Dynamic Memory Allocation. Suppose we have a program which can store 100 student’s marks. If you need to store 75 students’ marks in this program you can, but some memory are not uses. But if you need store 110 students’ marks in this program you can not do it. You need increase array size. So it is big problem when any body use this program because he or she can not increase the array size. If we have a way that when the program runs in that time we give the array size, then that problem can not come. Yes we have a way that when we run the program we give the student number then the program make array size in its own. The way is dynamic memory allocation. In this system we can use two functions they are malloc and calloc. They both do the same work. They are often known as “Dynamic memory allocation functions”. Let us now see a program, which uses the concept of dynamic memory allocation.

/*Program for dynamic memory allocation */
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,avg,I,*p,sum=0;

printf(“\n Enter the number of student: ”);
scanf(“%d”,&n);

p=(int*) malloc (n*2)
if(p==NULL)
{
printf(“\n Memory allocation unsuccessful”);
exit();
}
for(i=0;i<n;i++)
scanf(“%d”,(p+i));

for(i=0;i<n;i++)
sum=sum+*(p+i);

avg=sum /n;
printf(“Average marks=%d”,avg);
getch();
}
Note: here (n*2) means that the number of (student * 2). We want to convert the student number in byte. The number of student is integer so we multiply with 2(integer =2 byte). If it is float then we multiply with 4(float=4 byte).

Here, we have first asked for the number of students whose marks are to be entered and then allocated only as much memory as is really required to store these marks. Not a byte more, not a byte less. The allocation job is done using the standard library function malloc( ). malloc( ) returns a NULL if memory allocation is unsuccessful. If successful it returns the address of the memory chunk that is allocated. We have collected this address in an integer pointer p since malloc( ) returns a void pointer we have telecasted it into an integer pointer. IN the first for loop using simple pointer arithmetic we have stored the marks entered from keyboard into the memory that has been allocated. In the second for loop we have accessed the same values to find the average marks.

The calloc( ) functions works exactly similar to malloc( ) except for the fact that is needs tow arguments. For example,
Int *p
P=(int*) calloc (10,2);

Here 10 mean the number of student. And 2 indicates that we wish to allocate memory for storing integers.


Written by Arnob.
Reference: “Understanding Pointers In C”

3 comments:

  1. A very good conceptual tutorial......shows the jest of the entire concept of dynamic memory allocation....good for understanding an developing further concepts

    ReplyDelete
  2. it give me a very good reason to get a on my assignment

    ReplyDelete

free counters