cs201- introduction to programming- lecture 24
Post on 18-Dec-2014
16 Views
Preview:
DESCRIPTION
TRANSCRIPT
Introduction to Introduction to ProgrammingProgramming
Lecture 24Lecture 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
Static Memory Static Memory AllocationAllocation
int i , j , k ;int i , j , k ;
char s [ 20 ] ;char s [ 20 ] ;
Compile Time Compile Time AllocationAllocation
Dynamic Dynamic Memory Memory
AllocationAllocation
HeapHeap
PointersPointers
void void PointerPointer
int *i ; int *i ;
char *s ;char *s ;
i is a pointer to an integer
void void *ptr ;*ptr ;
CastCast
void *ptr ;void *ptr ;
( int * ) ( int * ) ptr ;ptr ;
NULLNULL
calloc ( n , m ) calloc ( n , m ) ;;
Space in terms of numbers of elements
Space in terms of size each of elements
calloc ( 1000 , sizeof calloc ( 1000 , sizeof ( int ) ) ;( int ) ) ;
( int * ) calloc ( 1000 , sizeof ( int ) ) ;( int * ) calloc ( 1000 , sizeof ( int ) ) ;
void * calloc ( size_t n , size_t el-size ) ;void * calloc ( size_t n , size_t el-size ) ;
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 ( ) ;
void * void * malloc ( n ) ;malloc ( n ) ;
Number of bytes required
malloc (1000 *sizeof ( int ) ) ) ;malloc (1000 *sizeof ( int ) ) ) ;
malloc ( n ( sizeof ( float ) ) ) ;malloc ( n ( sizeof ( float ) ) ) ;
Static Memory Static Memory AllocationAllocation
#define MAXSTUDENT 100#define MAXSTUDENT 100
int Student [ MAXSTUDENT ] ;int Student [ MAXSTUDENT ] ;
Problem Problem statementstatement
Find the average age of Find the average age of
the students in your classthe students in your class
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++ ;
free ( iPtr ) free ( iPtr ) ;;
realloc(void *iPtr, size_t size);realloc(void *iPtr, size_t size);
UnreferenceUnreferenced Memoryd Memory
Memory Memory LeaksLeaks
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
}}
Dangling Dangling PointersPointers
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
Multi-Multi-taskingtasking
ReviewReview Dynamic Memory AllocationDynamic Memory Allocation
– Efficient usage of computers Efficient usage of computers
resourcesresources– Have to do memoryHave to do memory
managementmanagement
top related