CTSD-2 Presentation about dynamic memory allocation.pptx

kmrinank 108 views 14 slides May 27, 2024
Slide 1
Slide 1 of 14
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14

About This Presentation

Dynamic memory allocation in c


Slide Content

Memory Wastage 7 8 9 arr [7] Solution is Dynamic Memory Allocation. arr [0] arr [1] arr [2]

What is Dynamic Memory Allocation? Allocation of memory at runtime is called dynamic memory allocation. There are four functions in c to allocate memory dynamically.

malloc(): Syntax: Ptr = (datatype*)malloc(size); Example: Int * ptr ; ptr =(int*)malloc(20); (or) ptr =(int*)malloc(5* sizeof (int)); Example Program: int main(){ int * ptr,n ; printf (“Enter the value of n=“); scanf (“% d”,&n ); ptr =(int*)malloc(n* sizeof (int)); printf (“enter numbers=“); for(int i =0; i <n; ++ i ){ scanf (“%d”, p+i ); } free( ptr ); return 0; }

calloc (): Syntax: Ptr = (datatype*) calloc ( n,size ); Example: Int * ptr ; ptr =(int*) calloc (10,2); (or) ptr =(int*) calloc (10,sizeof(int)); Example Program: int main(){ int * ptr,n ; printf (“Enter the value of n=“); scanf (“% d”,&n ); ptr =(int*) calloc ( n,sizeof (int)); printf (“enter numbers=“); for(int i =0; i <n; ++ i ){ scanf (“%d”, p+i ); } free( ptr ); return 0; }

realloc (): Syntax: Ptr = (datatype*) realloc ( ptr,newsize ); Example: Int * ptr ; ptr =(int*)malloc(n* sizeof (int)); ptr =(int*) realloc (ptr,20); Example Program: int main(){ int * ptr,n ; printf (“Enter the value of n=“); scanf (“% d”,&n ); ptr =(int*)malloc(n* sizeof (int)); ptr =(int*) realloc (ptr,20); printf (“enter numbers=“); for(int i =0; i <n; ++ i ){ scanf (“%d”, p+i ); } free( ptr ); return 0; }

free(): Syntax: free( ptr ); Example: Int * ptr ; ptr =(int*)malloc(n* sizeof (int)); free( ptr ); Example Program: int main(){ int * ptr,n ; printf (“Enter the value of n=“); scanf (“% d”,&n ); ptr =(int*)malloc(n* sizeof (int)); printf (“enter numbers=“); for(int i =0; i <n; ++ i ){ scanf (“%d”, p+i ); } free( ptr ); return 0; }

Programs: Q) WAP to allocate 3 numbers in an array dynamically and find the sum of the 3 numbers allocated. #include<stdio.h>   #include<stdlib.h>   int  main(){      int   n,i ,* ptr,sum =0;          printf ("Enter number of elements: ");          scanf ("% d",&n );          ptr =( int *)malloc(n* sizeof ( int ));  //memory allocated using malloc          if ( ptr ==NULL)                              {              printf ("Sorry! unable to allocate memory");             exit(0);         }          printf ("Enter elements of array: ");          for ( i =0;i<n;++ i )         {              scanf ("%d", ptr+i );             sum+=*( ptr+i );         }          printf ("Sum=% d",sum );         free( ptr );      return  0;   } 

#include<stdio.h>   #include<stdlib.h>   int  main(){      int   n,i ,* ptr,sum =0;          printf ("Enter number of elements: ");          scanf ("% d",&n );          ptr =( int *)malloc(n* sizeof ( int ));  //memory allocated using malloc          if ( ptr ==NULL)                              {              printf ("Sorry! unable to allocate memory");             exit(0);         }          printf ("Enter elements of array: ");          for ( i =0;i<n;++ i )         {              scanf ("%d", ptr+i );             sum+=*( ptr+i );         }          printf ("Sum=% d",sum );         free( ptr );      return  0;   } 

By Mrinank Kavuri Akhil Agastya Raman Pushpender Ramesh
Tags