Untitled Document

Dynamic Memory Allocation


Consider an array declaration say int marks[100]; such a declaration would reserve 200 bytes in memory to store 100 integers, say it is to store the marks of 100 students. But when a program is executed we may want to store the marks of only say 50 students. That means we have made use of only 100 bytes and 100 bytes are wasted or if we want to store the marks of 200 students, it is not possible to store because the array size is only for storing the marks of 100 students. So, when we declare arrays in the beginning static memory allocation takes place. But memory can be allocated only at the time of execution of the program and this is called as dynamic allocation. This kind of allocation can be done by using the library function malloc() or calloc() and these are known as dynamic memory allocation function. Thus the functions malloc() and calloc() allocates block of memory.



malloc() : This allocates single block of memory
Syntax:
void * malloc (byte_size);
It takes the form ptr=(cast type *)malloc(byte_size)
Function malloc() returns a pointer to space for an object of size byte_size or null if the request cannot be satisfied. The space is not-initialized.

Example:
ptr=(int *) malloc(50*sizeof(int));

The above statement will allocate a block of 100 bytes of memory and it's starting address will be stored in the pointer variable of type int. The cast type specified is int.

calloc():This allocates multiple blocks of storage, each of same size.
Syntax:
void * calloc (n, element size);

It takes the form ptr=(cast type *)calloc(n,element_size);
Function calloc() returns a pointer to space for an array of n objects, each of size element_size or null if the request cannot be satisfied. The space is initialized to zero.

Example:
ptr=(int *)calloc(5,sizeof(int));


The above statement allocates contiguous space for 5 blocks each of size 2 bytes.

realloc():

This changes the size of the object pointed to by ptr to new_size
.

Syntax:

void * realloc (void *ptr, newsize);

It takes the form ptr=realloc(ptr,new_size);
Function realloc() changes the size of the object pointed to by ptr to new_size The contents will be unchanged up to the minimum of the old and new sizes. If the new size is larger, the new space is un initialized. realloc() returnes a pointer to the new space or null if the request cannot be satisfied in which case, *ptr is unchanged.

free(): This deallocates the space pointed to by ptr.
Syntax:
void free (void *ptr);

The function free() de-allocates the space pointed to by ptr, it does nothing if ptr is null. ptr must be a pointer to space previously allocated by malloc(), calloc() or realloc().

All the above dynamic memory allocation functions are defined in header files alloc.h or stdlib.h.

Example:
#include <stdio.h>
# include <conio.h>
# include <alloc.h>
void main ( )
{
int *ptr,n,i;
printf ("Enter the number of items \n");
scanf ("%d",&n);
ptr = (int*) malloc(n* sizeof(int));
printf("Enter %d number of elements\n",n);
for (i=0; i<n;i++)
{
scanf ("%d",ptr+i);
}
printf("Entered elements are\n");
for (i=0; i<n;i++)
printf ("%d\n",*(ptr+i));
}
getch();
}


Output:



The function malloc() returns a null if memory allocation is unsuccessful and if successful it returns the address of the memory chunc (block) that was allocated. This address in the above program is collected in the pointer variable ptr. The expression (int *) is used to type cast the address being returned as the address of the integer. So, in the above example, ptr will have the starting address of the memory location.
The calloc() function works exactly similar to malloc() except for the fact that it needs two arguments as against one argument required by malloc().



Example:
ptr = (int *) calloc (10,2);
dd>
In the example , (int *) indicates that we wish to allocate the memory for storing integers. 2 indicates the size of the data type i.e., integer, 10 indicates that we want to reserve space for storing 10 integers.
Another difference between malloc() and calloc() is that, by default memory allocated by malloc() contains garbage value whereas that allocated by calloc() contains all zeroes.

Write a program to read the marks of students and calculate the average marks. Use calloc() function to allocate the space.
# include <stdio.h>
# include <conio.h>
# include <alloc.h>
void main ( )
{
int *ptr, i, n, total = 0;
float avg = 0.0;
printf ("Enter the number of students \n");
scanf ("%d",&n);
ptr = (int*)calloc(n,2);
printf ("Enter the Marks for %d number of students \n",n);
for (i=0; i {
scanf ("%d",ptr+i);
total = total + *(ptr + i);
}
printf ("Marks are : \n");
for (i=0; i {
printf("%d\n",*(ptr + i));
}
avg = total/n;
printf ("Average = %f\n", avg);
getch();
}

Output





Structures and Unions << Previous
Next >>Command Line Parameters

Our aim is to provide information to the knowledge seekers.

Support us generously


comments powered by Disqus






click here


Natural Natural Natural Footer1