You are on page 1of 10

2

Dynamic Memory Allocation: -

There are two type of memory allocation


1. Compile time/Static memory allocation
2. Run time/Dynamic memory allocation
1. Compile time/Static memory allocation: -
In static memory allocation we allocate the memory at
the time of compilation we cannot change the memory at run
time. in Static memory allocation there is memory wastage in
static memory allocation we cannot increase or decrease the
size of memory.
Example: - int a,b;
int[20];
Drawback of static memory allocation: -
We cannot increase or decrease the size in static memory allocation
There is memory wastage

2. Run time/Dynamic memory allocation: -


To overcome the limitation of static memory allocation
we use dynamic memory allocation. In dynamic memory
allocation we can increase or decrease the size at run time so
there is no memory wastage.
We use different type of function for dynamic memory
allocation. We use <alloc.h> header file for dynamic memory
allocation.
We can also use <stdio.h> header file for dynamic
memory allocation.

DYNAMIC MEMORY ALLOCATION 1


2

Header file used in dynamic memory allocation: -


# include< studio.h >
# include< stdlib.h >
# include< alloc.h >
Dynamic memory allocation function: -
a) malloc( );
b) calloc( );
c) realloc( );
d) free( );

Difference and Similarity between static and dynamic memory


allocation: -

Similarity: -
S. No. Static Dynamic
1. This is used for memory This is also used for memory
allocation. allocation.

Difference: -
S. No. Property Static Dynamic
1. Allocation In static memory In dynamic memory
allocation we allocate allocation we allocate the
the memory at the time memory at run time
of compilation
2. Memory wastage There is memory There is no memory
wastage wastage
3. Size (memory) in static memory In dynamic memory
increase or allocation we cannot allocation we can
decrease increase/decrease the increase/decrease the
size of memory size of memory

DYNAMIC MEMORY ALLOCATION 2


2

Dynamic memory allocation function: -

a) malloc( ); function: -
Malloc means memory allocation. Using malloc( ); we can
allocate memory at run time using malloc( ); we can increase
or decrease memory size at run time we pass one argument in
malloc( );. Default initial value of malloc( ); garbage value.
Syntax: -
ptr = ( cost type*) malloc ( byte size );

Example: -
ptr = ( int*) malloc ( n* size of ( int ) );

b) calloc( ); function: -
Calloc means contiguous memory allocation. Calloc( ); is
used for dynamic memory allocation using calloc( ); we can
increase or decrease the memory size at run time it initialize
the memory with default value zero. It has two parameters or
arguments.
Syntax: -
ptr = ( cost type* ) calloc ( byte size );

Example: -
ptr = ( int* ) calloc ( n , size of ( int ) );

DYNAMIC MEMORY ALLOCATION 3


2

Difference between malloc ( ); and calloc ( );


S. No. Property Malloc( ); Calloc( );
1. Memory Malloc( ); means memory Calloc( ); means contiguous
allocation allocation. memory allocation.
2. Default Default initial value is Default initial value is zero
initial value garbage value
3. No. of In malloc( ); no. of In calloc( ); no. of argument
argument argument is 1. is 2.
4. Syntax ptr=(cost ptr=(cost type*)calloc(byte
type*)malloc(byte size); size);
5. Example ptr=(int*)malloc(n*size ptr=(int*)calloc(n,size
of(int)); of(int));

c) realloc ( ); function: -

Realloc ( ); function is used to change the memory size


allocated by calloc ( ); and malloc ( );
Realloc ( ); reallocate the memory .realloc ( ); deallocate
the old object pointed by the pointer(ptr) and return a pointer
to a new object that has the size specified by the size.
Syntax: -
ptr = realloc ( old memory, new memory );
Example: -
ptr = realloc ( abc , 20 );
using realloc( ); we can increase/decrease the old allocated size of
the memory

DYNAMIC MEMORY ALLOCATION 4


2

d) free( ); function: -

Free( ); is used to deallocate the memory previously


allocated by malloc( ); ,calloc( ); and relloc( );.
At the end of every program we have to deallocate the
allocated memory using malloc( ); , calloc( ); and realloc( );
Syntax: -

free ( pointer );

Example: -

free ( ptr );

Program: -
Write a C program for implementation of malloc ( ), calloc ( ), realloc( ),
and free( );
Prog: - #include<stdio.h>

DYNAMIC MEMORY ALLOCATION 5


2

#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
#include<string.h>
void main( )
{
char *ms;
ms=(char*)malloc(10*size of (char);
srtcpy(ms, “ I am in up”);
printf(“%s”,ms);
realloc(ms,40);
strcpy(ms, “I am in india”);
printf(“%s”,ms);
free(ms);
getch( );
}
Write a C program for implementation of dynamic memory allocation
function.
Prog: - #include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
#include<string.h>
void main( )
{
char *ms;
ms=(char*)calloc(10,size of(char));
strcpy(ms,"i am in up");
printf("%s", ms);
realloc(ms,40);
strcpy(ms,"i am in india");
printf("%s",ms);
free(ms);
getch( );
}
Write a c program for implementation of dynamic memory at where size of
memory is entered by user
Prog: - #include<stdio.h>
#include<conio.h>

DYNAMIC MEMORY ALLOCATION 6


2

#include<alloc.h>
#include<stdlib.h>
#include<string.h>
void main( )
{
Int n;
char *ms;
printf(“ enter the value of n” );
scanf(“%d”,&n);
ms=(char*)calloc(n,size of(char));
strcpy(ms,"i am in up");
printf("%s", ms);
realloc(ms,40);
strcpy(ms,"i am in india");
printf("%s",ms);
free(ms);
getch( );
}

Write a C program to allocate the memory of size 100 byte and store your
hobbies in this memory in this memory after that reallocate the memory by
1000 and store your aim of life and at last free the memory
Prog: - #include<stdio.h>

DYNAMIC MEMORY ALLOCATION 7


2

#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
#include<string.h>
void main( )
{
char *ms;
ms=(char*)malloc(100,size of(char));
strcpy(ms, I love listening to music);
printf("%s", ms);
realloc(ms,1000);
strcpy(ms,"i am in india");
printf("%s",ms);
free(ms);
getch( );
}

Write a C program for implementation of malloc( );


Prog: - #include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
#include<string.h>
void main( )
{
char *ms;
ms=(char*)malloc(30*size of(char));
strcpy(ms,"Tauseef akhtar yusuf zai");
printf("%s",ms);
free(ms);
getch( );
}

Write a C program for implementation of calloc( );


Prog: - #include<stdio.h>

DYNAMIC MEMORY ALLOCATION 8


2

#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
#include<string.h>
void main( )
{
char *ms;
ms=(char*)calloc(30,size of(char));
strcpy(ms,"Tauseef akhtar yusuf zai");
printf("%s", ms);
free(ms);
getch( );
}

Write a C program for implementation of realloc( );


Prog: - #include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
#include<string.h>
void main( )
{
char *ms;
ms=(char*)calloc(30,size of(char));
strcpy(ms,"Tauseef akhtar yusuf zai");
printf("%s", ms);
realloc(ms,60);
printf("%s", ms);
free(ms);
getch( );
}

Write a C program for implementation of free( );


Prog: - #include<stdio.h>
#include<conio.h>
#include<alloc.h>

DYNAMIC MEMORY ALLOCATION 9


2

#include<stdlib.h>
#include<string.h>
void main( )
{
char *ms;
ms=(char*)malloc(20,size of(char));
strcpy(ms,"computer science");
printf("%s", ms);
free(ms);
getch( );
}

DYNAMIC MEMORY ALLOCATION 10

You might also like