Thestudentsinyourclass intnumstd intiptrsptr

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Student [ MAXSTUDENT ] ; Problem statement Find the average age of the students in your class int numstd ; int *iPtr , *sPtr ; cout << "Enter the number of students " << endl ; cin >> numstd ; iPtr = malloc ( numstd * ( sizeof ( int ) ) ) ; if ( iPtr == NULL ) { cout << "Error on malloc " ; return 1 ; /* Use a nonzero return to indicate an error has occurred */ } // a while loop to read the ages of the student and place them in the memory sPtr = iPtr ; sPtr++ ; Example 2 free ( iPtr ) ; realloc(void *iPtr, size_t size); Unreferenced Memory Memory Leaks Example main ( ) { funct ( ) ; } funct ( ) { int *iPtr ; iPtr = malloc ( 1000 * ( sizeof ( int ) ) ) ; // used the memory } Dangling Pointers int *ptr1 , *ptr2 ; ptr1 = malloc (1000 * ( sizeof ( int ) ) ) ; ptr2 = ptr1 ; ­ ­ ­ free ( ptr1 ) ; Example Multi­tasking Dynamic Memory Allocation Review – Efficient usage of computers resources – Have to do memory management...
View Full Document

Ask a homework question - tutors are online