cs201- introduction to programming- lecture 24

34
Introduction to Introduction to Programming Programming Lecture 24 Lecture 24

Upload: bilal-ahmed

Post on 18-Dec-2014

16 views

Category:

Education


3 download

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

Introduction to Introduction to ProgrammingProgramming

Lecture 24Lecture 24

Page 2: CS201- Introduction to Programming- Lecture 24

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

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

Compile Time Compile Time AllocationAllocation

Page 5: CS201- Introduction to Programming- Lecture 24

Dynamic Dynamic Memory Memory

AllocationAllocation

Page 6: CS201- Introduction to Programming- Lecture 24

HeapHeap

Page 7: CS201- Introduction to Programming- Lecture 24

PointersPointers

Page 8: CS201- Introduction to Programming- Lecture 24

void void PointerPointer

Page 9: CS201- Introduction to Programming- Lecture 24

int *i ; int *i ;

char *s ;char *s ;

i is a pointer to an integer

Page 10: CS201- Introduction to Programming- Lecture 24

void void *ptr ;*ptr ;

Page 11: CS201- Introduction to Programming- Lecture 24

CastCast

Page 12: CS201- Introduction to Programming- Lecture 24

void *ptr ;void *ptr ;

Page 13: CS201- Introduction to Programming- Lecture 24

( int * ) ( int * ) ptr ;ptr ;

Page 14: CS201- Introduction to Programming- Lecture 24

NULLNULL

Page 15: CS201- Introduction to Programming- Lecture 24

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

calloc ( 1000 , sizeof calloc ( 1000 , sizeof ( int ) ) ;( int ) ) ;

Page 17: CS201- Introduction to Programming- Lecture 24

( int * ) calloc ( 1000 , sizeof ( int ) ) ;( int * ) calloc ( 1000 , sizeof ( int ) ) ;

Page 18: CS201- Introduction to Programming- Lecture 24

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

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

void * void * malloc ( n ) ;malloc ( n ) ;

Number of bytes required

Page 21: CS201- Introduction to Programming- Lecture 24

malloc (1000 *sizeof ( int ) ) ) ;malloc (1000 *sizeof ( int ) ) ) ;

Page 22: CS201- Introduction to Programming- Lecture 24

malloc ( n ( sizeof ( float ) ) ) ;malloc ( n ( sizeof ( float ) ) ) ;

Page 23: CS201- Introduction to Programming- Lecture 24

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

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

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

free ( iPtr ) free ( iPtr ) ;;

Page 27: CS201- Introduction to Programming- Lecture 24

realloc(void *iPtr, size_t size);realloc(void *iPtr, size_t size);

Page 28: CS201- Introduction to Programming- Lecture 24

UnreferenceUnreferenced Memoryd Memory

Page 29: CS201- Introduction to Programming- Lecture 24

Memory Memory LeaksLeaks

Page 30: CS201- Introduction to Programming- Lecture 24

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

Dangling Dangling PointersPointers

Page 32: CS201- Introduction to Programming- Lecture 24

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

Multi-Multi-taskingtasking

Page 34: CS201- Introduction to Programming- Lecture 24

ReviewReview Dynamic Memory AllocationDynamic Memory Allocation

– Efficient usage of computers Efficient usage of computers

resourcesresources– Have to do memoryHave to do memory

managementmanagement