Professional Documents
Culture Documents
Dynamic Memory Allocation Pps
Dynamic Memory Allocation Pps
Dynamic Memory Allocation Pps
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
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 ) );
c) realloc ( ); function: -
d) free( ); function: -
free ( pointer );
Example: -
free ( ptr );
Program: -
Write a C program for implementation of malloc ( ), calloc ( ), realloc( ),
and free( );
Prog: - #include<stdio.h>
#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>
#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>
#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( );
}
#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( );
}
#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( );
}