cs201- introduction to programming- lecture 24
DESCRIPTION
Virtual University Course CS201- Introduction to Programming Lecture No 24 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]TRANSCRIPT
![Page 1: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/1.jpg)
Introduction to Introduction to ProgrammingProgramming
Lecture 24Lecture 24
![Page 2: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/2.jpg)
Today’s Today’s AgendaAgenda Memory AllocationMemory Allocation
– Dynamic memory allocationDynamic memory allocation– Advantages/disadvantages of Advantages/disadvantages of
Dynamic and static memory Dynamic and static memory allocationallocation
– Common programming errors Common programming errors while using Dynamic memory while using Dynamic memory allocationallocation
![Page 3: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/3.jpg)
Static Memory Static Memory AllocationAllocation
int i , j , k ;int i , j , k ;
char s [ 20 ] ;char s [ 20 ] ;
![Page 4: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/4.jpg)
Compile Time Compile Time AllocationAllocation
![Page 5: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/5.jpg)
Dynamic Dynamic Memory Memory
AllocationAllocation
![Page 6: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/6.jpg)
HeapHeap
![Page 7: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/7.jpg)
PointersPointers
![Page 8: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/8.jpg)
void void PointerPointer
![Page 9: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/9.jpg)
int *i ; int *i ;
char *s ;char *s ;
i is a pointer to an integer
![Page 10: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/10.jpg)
void void *ptr ;*ptr ;
![Page 11: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/11.jpg)
CastCast
![Page 12: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/12.jpg)
void *ptr ;void *ptr ;
![Page 13: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/13.jpg)
( int * ) ( int * ) ptr ;ptr ;
![Page 14: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/14.jpg)
NULLNULL
![Page 15: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/15.jpg)
calloc ( n , m ) calloc ( n , m ) ;;
Space in terms of numbers of elements
Space in terms of size each of elements
![Page 16: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/16.jpg)
calloc ( 1000 , sizeof calloc ( 1000 , sizeof ( int ) ) ;( int ) ) ;
![Page 17: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/17.jpg)
( int * ) calloc ( 1000 , sizeof ( int ) ) ;( int * ) calloc ( 1000 , sizeof ( int ) ) ;
![Page 18: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/18.jpg)
void * calloc ( size_t n , size_t el-size ) ;void * calloc ( size_t n , size_t el-size ) ;
![Page 19: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/19.jpg)
Example 1Example 1int *iPtr ;int *iPtr ;
iPtr = ( int * ) calloc ( 1000 , sizeof ( int ) ) ;iPtr = ( int * ) calloc ( 1000 , sizeof ( int ) ) ;
if ( iPtr == NULL )if ( iPtr == NULL )
exit ( ) ;exit ( ) ;
![Page 20: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/20.jpg)
void * void * malloc ( n ) ;malloc ( n ) ;
Number of bytes required
![Page 21: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/21.jpg)
malloc (1000 *sizeof ( int ) ) ) ;malloc (1000 *sizeof ( int ) ) ) ;
![Page 22: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/22.jpg)
malloc ( n ( sizeof ( float ) ) ) ;malloc ( n ( sizeof ( float ) ) ) ;
![Page 23: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/23.jpg)
Static Memory Static Memory AllocationAllocation
#define MAXSTUDENT 100#define MAXSTUDENT 100
int Student [ MAXSTUDENT ] ;int Student [ MAXSTUDENT ] ;
![Page 24: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/24.jpg)
Problem Problem statementstatement
Find the average age of Find the average age of
the students in your classthe students in your class
![Page 25: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/25.jpg)
Example 2Example 2int numstd ;int numstd ;int *iPtr , *sPtr ;int *iPtr , *sPtr ;cout << "Enter the number of students " << endl ;cout << "Enter the number of students " << endl ;cin >> numstd ;cin >> numstd ;iPtr = malloc ( numstd * ( sizeof ( int ) ) ) ;iPtr = malloc ( numstd * ( sizeof ( int ) ) ) ;if ( iPtr == NULL )if ( iPtr == NULL ){{ cout << "Error on malloc " ; cout << "Error on malloc " ; return 1 ; return 1 ; /* Use a nonzero return to indicate an error has occurred *//* Use a nonzero return to indicate an error has occurred */}}
// a while loop to read the ages of the student and place them in // a while loop to read the ages of the student and place them in the memorythe memory sPtr = iPtr ;sPtr = iPtr ; sPtr++ ;sPtr++ ;
![Page 26: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/26.jpg)
free ( iPtr ) free ( iPtr ) ;;
![Page 27: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/27.jpg)
realloc(void *iPtr, size_t size);realloc(void *iPtr, size_t size);
![Page 28: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/28.jpg)
UnreferenceUnreferenced Memoryd Memory
![Page 29: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/29.jpg)
Memory Memory LeaksLeaks
![Page 30: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/30.jpg)
Example Example main ( )main ( ){{
funct ( ) ;funct ( ) ;}}funct ( )funct ( ){{ int *iPtr ;int *iPtr ;
iPtr = malloc ( 1000 * ( sizeof ( int ) ) ) ; iPtr = malloc ( 1000 * ( sizeof ( int ) ) ) ; // used the memory// used the memory
}}
![Page 31: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/31.jpg)
Dangling Dangling PointersPointers
![Page 32: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/32.jpg)
int *ptr1 , *ptr2 ;int *ptr1 , *ptr2 ;
ptr1 = malloc (1000 * ( sizeof ( int ) ) ) ; ptr1 = malloc (1000 * ( sizeof ( int ) ) ) ;
ptr2 = ptr1 ;ptr2 = ptr1 ;
- - -- - -
free ( ptr1 ) ;free ( ptr1 ) ;
ExampleExample
![Page 33: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/33.jpg)
Multi-Multi-taskingtasking
![Page 34: CS201- Introduction to Programming- Lecture 24](https://reader037.vdocuments.site/reader037/viewer/2022102922/5492e761b47959610c8b48c1/html5/thumbnails/34.jpg)
ReviewReview Dynamic Memory AllocationDynamic Memory Allocation
– Efficient usage of computers Efficient usage of computers
resourcesresources– Have to do memoryHave to do memory
managementmanagement