0% found this document useful (0 votes)
21 views

Dynamic Memory Allocation

Dynamic memory allocation (DMA) allows programs to allocate memory at runtime using functions like malloc(), calloc(), realloc(), and free(). Malloc() allocates unused memory on the heap and returns a pointer to it. Calloc() allocates memory for arrays and initializes values to 0. Realloc() changes the size of previously allocated memory. Free() releases memory to prevent waste. DMA avoids unused space in static allocation and allows flexible memory usage at runtime.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Dynamic Memory Allocation

Dynamic memory allocation (DMA) allows programs to allocate memory at runtime using functions like malloc(), calloc(), realloc(), and free(). Malloc() allocates unused memory on the heap and returns a pointer to it. Calloc() allocates memory for arrays and initializes values to 0. Realloc() changes the size of previously allocated memory. Free() releases memory to prevent waste. DMA avoids unused space in static allocation and allows flexible memory usage at runtime.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Dynamic memory

allocation
Dynamic Memory Allocation (DMA):
Create memory at runtime by the programmer is known as “Dynamic Memory
Allocation”. In static memory allocation some amount of space is unused. This unused space is
empty. But it is filled with garbage values.
To avoid this problem, DMA technique was introduced. DMA allows to allocates
memory at run time. So it needs to no memory will be wastage. Here we are using different
functions for manipulate memory. They are
1. malloc()
2. calloc()
3. realloc()
4. free()
malloc():
This function is used to allocate memory space in form of bytes to the variables that
variables are different data types. This functions reserves size of bytes and returns a pointer to
some amount of memory of size. The memory pointed to will be on the heap, not the stack. so
make sure to free it when you are doing this.
Syn: pointer variable=(data type*) malloc (given size);
Ex: ptr=(int*) malloc (n);
The following program illustrated about malloc().
#include<alloc.h>
void main()
{
int *a, n, i ;
printf(“\n enter range of an array:”);
scanf(“%d”,&n);
a=(int*)malloc(n);
for(i=0;i<n;i++)
{
scanf(“%d”, (a+i));
}
printf(“ the array elements are :”);
for(i=0;i<n;i++)
{

Page 7
printf(“%d”,*(a+i));
}
}
calloc():
This function is useful for allocating multiple blocks of memory. It is declared with two
arguments. This function is usually used for allocating memory for array and structure. The
calloc() can be used in place of malloc() function.and returns starting address of the memory to
the pointer variable.
Syn: pointer variable=(data type*) calloc(n,2);
Ex: ptr= (int*)calloc(4,2);
the above declaration allocates 4 blocks of memory, each block contains 2 bytes.
The following program illustrates calloc().
#include<alloc.h>
void main()
{
int *a, n, i ;
printf(“\n enter range of an array”);
scanf(“%d”,&n);
a=(int *) calloc ( n,2);
printf(“\n enter elements”);
for(i=0;i<n;i++)
{
scanf(“%d”,(a+i));
}
printf(“\n the array elements are:”);
for(i=0;i<n;i++)
{
printf(“%d”, *(a+i));
}
}

Page 8
realloc():
This realloc() function is used to reallocates the memory. And also enlarge the previously
allocated memory by malloc() or calloc() functions. It returns the address of the reallocated
block, which can be different from the original address. If the block can not be reallocated,
realloc () returns NULL.
Syn: pointer variable name=(data type *) realloc( pointer name, new size);
The following program illustrates about realloc() function
#include<alloc.h>
#include<string.h>
void main()
{
char *s;
s=(char*)malloc (6);
s=”bvirice”;
printf(“%s”, s);
s=(char *) realloc( s, 15);
s=”welcome to bvrice”;
printf(“%s”,s );
}
free():
the free() function is used to release the memory allocated by memory allocating functions.
Thus, using this function wastage of memory is prevented.
Syn: free(pointer variable);
The following program illustrates about free()
#include<stdlib.h>
#include<string.h>
void main()
{
char *s;
s=( char*) malloc(6);
s=”Vishnu”;
printf(“%s”, s);
s=(char *)realloc ( s, 20);
Page 9
s=”welcome to bvrice”;
printf( “%s”, s);
free(s);
}

You might also like