Click to edit Master title style,Click to edit Master text styles,Second level,Third level,Fourth level,Fifth level,*,Lecture 18:Dynamic Memory Allocation,Lecture 18,Accessing 2-Dimensional Array+Dynamic Memory Allocation(DMA),Dynamic Memory Allocation,Used when,space requirements,are unknown at,compile time,.,Most of the time,the amount of space required,is unknown at compile time,.,For example,consider the library database example,what about 201th book information,.,Using static memory allocation it is impossible,.,Dynamic Memory Allocation(DMA):-,With Dynamic memory allocation we can allocate/deletes memory(elements of an array),at runtime or execution time,.,Three Functions for DMA operations in C language.Include,or,files before using,malloc,realloc,free,DMA using malloc(1/3),Syntax,void*,malloc(size_t,size),Return value:,On success,malloc,returns a pointer to the newly allocated block of memory,.,On error,(if not enough space exists for the new block),malloc,returns null,.,If the argument size=0,malloc,returns null,.,Returns the address of newly allocated memory of any data type(int/float/long/double/char),In bytes,malloc(3)means you are allocation only 3 bytes,DMA using malloc(2/3),int*ip;,ip=(int*)malloc(20);,if(ip=NULL),printf(“n Pointer is NULL);,else,printf(“n Pointer is not NULL);,0,1,2,3,4,5,6,7,8,9,*ip,DMA using malloc(3/3),Example1:-,(Allocating float type memory),ip=,(float*),malloc(40);,/allocates 10 float type slots.,Example2:-(,Allocating double type memory),ip=,(double*),malloc(80);,/allocates 10 double type slots.,Example3:-,(Allocating long type memory),ip=,(long*),malloc(40);,/allocates 10 long type slots.,Example4:-,(Allocating char type memory),ip=,(char*),malloc(10);,/allocates 10 char type slots.,Assigning/Accessing data in DMA(1/3),void main(void),float*DMA;,DMA=(float*)malloc(40);,/10 slots are allocated,float*pointer=DMA;,/assign first element address,*pointer=29.53;,pointer+=7;,*pointer=67.23;,printf(“The value at 7,th,location is%d“,*pointer);,0,1,2,3,4,5,6,7,8,9,29.53,67.23,Assigning/Accessing data in DMA(Alternative way)(2/3),void main(void),float*DMA;,DMA=(float*)malloc(40);,/10 slots are allocated,DMA0=29.53;,DMA7=67.23;,printf(“The value at 7,th,location is%d“,DMA7);,0,1,2,3,4,5,6,7,8,9,29.53,67.23,Assigning/Accessing data in DMA(Alternative way)(3/3),void main(void),char*DMA;,DMA=(char*)malloc(10);/10 slots are allocated,strcpy(DMA,“Pakistan);,printf(“The text in DMA is%s“,DMA);,0,1,2,3,4,5,6,7,8,9,P,a,k,i,s,t,a,n,/0,Problem with DMA using malloc,The major problem with DMA using malloc is that,when,it is recursively called upon,same pointer type variable,.,It removes the old information.,For Example:-,int*ip;,ip=(int*)malloc(22);,/11 elements memory,ip0=3;ip6=6;,ip=(int*)malloc(26);,/13 elements memory,0,1,2,3,4,5,6,7,8,9,10,3,6,*ip,*ip,0,1,2,3,4,5,6,7,8,9,10,11,12,DMA using realloc,void*,realloc(void,*,ptr,int,n),If the block needs to be extended and it can be,realloc,allocates a new block of size n,copies over the old,information,and frees(destroy)the old block,If the,n,is given smaller than old size,then the memory is shrink.,If,ptr,is NULL,its the same as,malloc,.,Returns the address of newly allocated memory any data type(int/float/long/double/char),In bytes,realloc(3)means you are allocation only 3 bytes,Which memory is needed to,resize,.,int*ip;,ip=(int*)malloc(22);,ip0=3;,ip6=6;,ip=(int*)realloc(ip,26);,DMA using realloc(Example),0,1,2,3,4,5,6,7,8,9,10,3,6,*ip,*ip,0,1,2,3,4,5,6,7,8,9,10,3,6,11,12,sizeof Operators,sizeof operator,Returns size of operand in bytes,For arrays,sizeof returns,(size of 1 element)*(number of elements),If sizeof(int)returns 2 then int myArray 10;printf(“%d,sizeof(myArray);,will print 20,Can be used with,Variable names,Type names,Constant values,int*array;,int row=6;,int col=10;,array=(int*)malloc(row*sizeof(int);,for(int loop=0;loop row;loop+),arrayloop=(int*)malloc(col*sizeof(int);,DMA on 2-Dimensional Array,Casting pointers(1/3),Pointers have types,so you cant just do,int,*pi;,double,*pd;,pd=pi;,Even though they are both just integers,C wont let(,Error,),Casting pointers(2/3),C will let you change the type of a pointer with an,explicit cast,int,*pi;double*pd;,pd=,(double*),pi;,Deallocation in C language,void free(void*ptr),int*pi;,pi=(int*)malloc(20);,free(pi);,Takes a pointer of any type,and deletes its entry in the“allocated list,Called upon NULL memory terminates your software.,Memory Leaks,Memory leaks occur when the programmer loses track of memory allocated by,malloc,or,realloc,.,void foo(void),char*ca=malloc(.);,/*no free*/,exit(1);,Memory Leaks,Memory Leak problem.,void foo(void),int*pointer;,for(int loop=0;loop 200000;loop+),pointer=(int*)realloc(pointer,200000);,