120 likes | 211 Views
Introduction to C Programming CE00312-1. Lecture 18 Dynamic Memory Allocation and Ragged Arrays. Allocation of Static Memory. A declaration such as char name[1000][21], allocates sufficient memory for 1000 names of 21 characters each.
E N D
Introduction to C ProgrammingCE00312-1 Lecture 18 Dynamic Memory Allocation and Ragged Arrays
Allocation of Static Memory • A declaration such as char name[1000][21], allocates sufficient memory for 1000 names of 21 characters each. • This storage of 21 000 characters is reserved at compilation time, i.e. before the program begins execution. • This wastes space as most names are rather less than 20 letters. • It also restricts names to a maximum of 20 characters. • We can resolve this wastage and restriction by allocation of memory as and when required, during execution of the program.
Dynamic Allocation using malloc function • A standard function, malloc(n)allocates n bytes (for n characters) in memory and returns a pointer to it. For example: the following code • Prompts the user for the size of the string • Reads the size • Allocates memory for a string of this size • Reads the string
Example using malloc int size; char *s; printf(“\nhow many characters?\n”); scanf(“%d”, &size); s = malloc(size+1); // one extra for ‘\0’ printf(“type string\n”); gets(s); allocates only enough memory for the expected string.
Use of malloc for any data type malloc can be used to allocate memory for any <data type>. <data type> *<variable>; // pointer to <data type> // some other code here <variable> = (<data type> *)malloc(n * sizeof(<data type>)); allocates sufficient memory for n values of <data type> and returns a pointer which must be casted as the same pointer type as the <variable>.
Using malloc for an array of integers For example: To allocate storage for 10 integers int *p; p = (int *) malloc (10 * sizeof(int)); This is machine-independent, as sizeof returns size of an integer for any machine.
Ragged Arrays Declare 6 variable length names – array of string pointers char *name[6] = {“abdul”, “abraham”, “al “, “bill”, “fred”, “jean-pierre”}; We can declare functions on ragged arrays: void print_list(char *table1[ ], int n) so that it can deal with strings of any length.
Ragged array example Read a list of strings (one on each line) into an array where the size of each element is precisely the length of each string, ie the strings are of variable length - a ragged array. #include "stdio.h" #include "string.h" #include "stdlib.h" // for malloc // prototypes void sort(char *[], int); int read_strings(char *[], int); void print_strings(char *[], int);
Main function int main(void) { char *list[1000]; // array of 1000 string pointers int n; // actual number of strings n = read_strings(list, 1000); // read n strings, max of 1000 sort(list, n); // sort n strings print_strings(list, n); // print n strings return (0); }
print_strings function void print_strings(char *list[], int n) { int i; printf("\nalphabetical order is\n\n"); for (i=0; i<n; i++) { printf("%s\n", list[i]); } }
void sort(char *a[], int n) { // array of n strings as pointers int i, j; char *temp; // string pointer for (j = n -1; j > 0; j--) { // each pass of j comparisons for (i=0; i < j; i++) { // each comparison if (strcmp(a[i], a[i+1]) > 0) { // swap the POINTERS temp = a[i]; a[i] = a[i+1]; a[i+1] = temp; } // else no swap } // end of pass } // end of all passes } // end sort
int read_strings(char *list[], int max) { int i = 0; char line[81]; // store for each line printf("\ntype one string per line\n"); while (gets(line) != NULL && i < max) { // while not end of input list[i] = malloc (strlen(line)+1); // list[i] big enough // for line + 1 for ‘\0’ strcpy(list[i], line); // copy line into list[i] i++; // next element } return i; // actual number of strings }