Cs201 24

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: Introduction to Programming Lecture 24 Memory Allocation Today’s Agenda – Dynamic memory allocation – Advantages/disadvantages of Dynamic and static memory allocation – Common programming errors while using Dynamic memory allocation int i , j , k ; char s [ 20 ] ; Static Memory Allocation Compile Time Allocation Dynamic Memory Allocation Heap Pointers void Pointer int *i ; char *s ; i is a pointer to an integer void *ptr ; Cast void *ptr ; ( int * ) ptr ; NULL Space in terms of numbers of elements calloc ( n , m ) ; Space in terms of size each of elements calloc ( 1000 , sizeof ( int ) ) ; ( int * ) calloc ( 1000 , sizeof ( int ) ) ; void * calloc ( size_t n , size_t el­size ) ; Example 1 int *iPtr ; iPtr = ( int * ) calloc ( 1000 , sizeof ( int ) ) ; if ( iPtr == NULL ) exit ( ) ; void * malloc ( n ) ; Number of bytes required malloc (1000 *sizeof ( int ) ) ) ; malloc ( n ( sizeof ( float ) ) ) ; Static Memory Allocation #define MAXSTUDENT 100 int...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online