Dynamic Memory Allocation Structure pointers Lecture 17 b
Dynamic Memory Allocation, Structure pointers Lecture 17 b 20. 3. 2001. Sudeshna Sarkar, CSE, IIT Kharagpur 1
Basic Idea n n Many a time we face situations where data is dynamic in nature. n Amount of data cannot be predicted beforehand. n Number of data item keeps changing during program execution. Such situations can be handled more easily and effectively using dynamic memory management techniques. 20. 3. 2001. Sudeshna Sarkar, CSE, IIT Kharagpur 2
n C language requires the number of elements in an array to be specified at compile time. n n Often leads to wastage or memory space or program failure. Dynamic Memory Allocation n n Memory space required can be specified at the time of execution. C supports allocating and freeing memory dynamically using library routines. 20. 3. 2001. Sudeshna Sarkar, CSE, IIT Kharagpur 3
Memory Allocation Process in C Local variables Stack Free memory Heap Global variables Instructions 20. 3. 2001. Permanent storage area Sudeshna Sarkar, CSE, IIT Kharagpur 4
n n n The program instructions and the global variables are stored in a region known as permanent storage area. The local variables are stored in another area called stack. The memory space between these two areas is available for dynamic allocation during execution of the program. n n 20. 3. 2001. This free region is called the heap. The size of the heap keeps changing Sudeshna Sarkar, CSE, IIT Kharagpur 5
Memory Allocation Functions n malloc: Allocates requested number of bytes and n returns a pointer to the first byte of the allocated space. calloc: Allocates space for an array of elements, initializes them to zero and then returns a pointer to the memory. free : Frees previously allocated space. n realloc: Modifies the size of previously allocated n space. 20. 3. 2001. Sudeshna Sarkar, CSE, IIT Kharagpur 6
Dynamic Memory Allocation n used to dynamically create space for arrays, structures, etc. int main () { int *a ; int n; . . a = (int *) calloc (n, sizeof(int)); . . } 20. 3. 2001. a = malloc (n*sizeof(int)); Sudeshna Sarkar, CSE, IIT Kharagpur 7
n n Space that has been dynamically allocated with either calloc() or malloc() does not get returned to the function upon function exit. The programmer must use free() explicitly to return the space. n n 20. 3. 2001. ptr = malloc (. . . ) ; free (ptr) ; Sudeshna Sarkar, CSE, IIT Kharagpur 8
void read_array (int *a, int n) ; int sum_array (int *a, int n) ; void wrt_array (int *a, int n) ; int main () { int *a, n; printf (“Input n: “) ; scanf (“%d”, &n) ; a = calloc (n, sizeof (int)) ; read_array (a, n) ; wrt_array (a, n) ; printf (“Sum = %dn”, sum_array(a, n); } 20. 3. 2001. Sudeshna Sarkar, CSE, IIT Kharagpur 9
void read_array (int *a, int n) { int i; for (i=0; i<n; i++) scanf (“%d”, &a[i]) ; } void sum_array (int *a, int n) { int i, sum=0; for (i=0; i<n; i++) sum += a[i] ; return sum; } void wrt_array (int *a, int n) { int i; . . . . } 20. 3. 2001. Sudeshna Sarkar, CSE, IIT Kharagpur 10
Arrays of Pointers n Array elements can be of any type n n 20. 3. 2001. array of structures array of pointers Sudeshna Sarkar, CSE, IIT Kharagpur 11
int main (void) { char word[MAXWORD]; char * w[N]; /* an array of pointers */ int i, n; /* n: no of words to sort */ for (i=0; scanf(“%s”, word) == 1); ++i) { w[i] = calloc (strlen(word)+1, sizeof(char)); if (w[i] == NULL) exit(0); strcpy (w[i], word) ; } n = i; sortwords (w, n) ; wrt_words (w, n); return 0; } 20. 3. 2001. Sudeshna Sarkar, CSE, IIT Kharagpur 12
Input : A is for apple or alphabet pie which all get a slice of come taste it and try w 0 1 2 A i s f o r 3 a p p l e 17 t r y 20. 3. 2001. Sudeshna Sarkar, CSE, IIT Kharagpur 13
void sort_words (char *w[], int n) { int i, j; for (i=0; i<n; ++i) for (j=i+1; j<n; ++j) if (strcmp(w[i], w[j]) > 0) swap (&w[i], &w[j]) ; } void swap (char **p, char **q) { char *tmp ; tmp = *p; *p = *q; *q = tmp; } 20. 3. 2001. Sudeshna Sarkar, CSE, IIT Kharagpur 14