Before we start - Announcements: There will be a LAB TONIGHT from 5:30 6:30 in CAMP 172 The lab will be on pointers In compensation, no class on Friday, Jan. 31. 1
Consider the bubble function one more time - void bubble (int a[ ], int arraysize) int i, j; int temp; for (i = 0; i < (arraysize - 1); i++) for (j = 0; j < (arraysize - (i+1)); j++) if (a[j] < a[j+1]) temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; This thing can only Sort arrays of int Type things! 2
What if we wanted to generalize the sorting function so that it worked with arrays of any simple type? Write a function template for it: template <typename T> void bubble1 (T a[], int arraysize) int i, j; T temp; for (i = 0; i < (arraysize - 1); i++) for (j = 0; j < (arraysize - (i+1)); j++) if (a[j] < a[j+1]) temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; 3
and use the template function in your code: template <typename T> void bubble1(t a[], int arraysize); int main() double x1[5] = 5.5, 3.32, -1.7, 10.11, 2.2; int y1[7] = 9, 1, 6, -13, 22, 5, 15; int i; bubble1 (x1, 5); cout << " sorted contents of array x1: " ; for (i = 0; i < 5; i++) cout << " " << x1[i]; cout << endl << endl; bubble1(y1, 7); cout << " sorted contents of array y1: " ; for (i = 0; i < 7; i++) cout << " " << y1[i]; cout << endl; return 0; 4
Dynamic Arrays Standard C++ arrays are static things: Once created, they stay the same size the declaration const int SIZE = 5; double nums[size]; tells the compiler: that a block of memory big enough to hold 5 double values has to be allocated and the variable nums is to store the starting address of this block Standard C++ arrays don t know their own size Most C++ compilers do not by default check whether indices are in range at runtime Sometimes situations arise in which we don t know the size an array should be until runtime. We may want to enter the array s size at runtime use dynamic arrays 5
What do we need in order to have dynamic arrays? When your program is running, there is part of memory that is unused, and this space is called the heap or free store To deal with dynamic arrays, we need: a mechanism for acquiring new memory locations when they are needed the C++ operator new is used to request a block of memory at runtime a way to release memory locations when they are no longer needed the C++ operator delete is a request for the system to return memory to the heap where it can be used in later reallocation 6
The new operator syntax: new int[6]; new double[10]; what does it do? requests allocation of a block of memory of proper size if the request can be satisfied, new returns the starting address of the block of memory usage: char * cptr; cptr = new char[20]; // cptr is a pointer to chars // now cptr points to // a block of memory big // enough for 20 chars 7
Example Here we first make an array of integers 5 long, give it values and print it int *array1 = NULL; Then we deallocate it int size = 5; Then we do the same thing for an array of size 10 array1 = new int[size]; for (int i = 0; i < size; i++) array1[i] = i; cout << "value of array1 is " << array1 << " and size is " << size << endl; for (int i = 0; i < size; i++) cout << "i = " << i << " and array1[i] = " << array1[i]<< endl; cout << endl; delete[] array1; array1 = NULL; size = 10; array1 = new int[size]; for (int i = 0; i < size; i++) array1[i] = i+25; cout << " value of array1 is: " << array1 << " and size is " << size << endl; for (int i = 0; i < size; i++) cout << "i = " << i << " and array1[i] = " << array1[i] << endl; cout << endl; Notice that the array is referenced through its pointer just as though it were a static array, but it isn t 8
Example: Words of varying length int length; char * word = NULL; char ans = 'y'; while (ans == 'y') cout << "Enter the word length: "; cin >> length; word = new char[length + 1]; for (int i = 0; i < length; i++) cin >> word[i]; word[length] = '\0'; cout << endl << "The word entered is: " << word; delete [] word; cout << endl << "Want to enter another word? "; cin >> ans; Notice that this code is not particularly robust! It s really easy to break!! What happens if this statement is not there? 9
A Little More on Arrays and Pointers Arrays and pointers are very closely related concepts Consider the following code snippet int numbers[25]; int *p; We ve declared an array of 25 integers and a pointer to int Now consider the following statement p = numbers; The assignment is perfectly legal But this one is not: numbers = p; The array name is a constant pointer can t be changed 10
Why are Pointers Important? You used reference parameters and references last semester, so why bother with pointers? Pointers to data improve performance for repetitive operations such as traversing strings and lists it is much cheaper in time and space to copy and dereference pointers than it is to copy and access the data to which the pointers point Pointers are used to hold the addresses of entry points for called functions and for run-time linking to dynamic link libraries (DLLs) In Object-oriented programming, pointers to functions are used for binding methods Basic concept in polymorphism the single most powerful contribution of Object Oriented programming 11
One More Example: Arrays of C-strings More Dynamic Data char aword[25]; char* words[50]; int n = 0; // number of words cout << "enter a set of words; terminate by entering #" << endl; while (cin >> aword) if (aword[0]!= '#') int len = strlen(aword) + 1; // amount of space needed char * space = new char[len]; // allocate enough space strcpy (space, aword); // copy into the new space words[n] = space; n++; else break; cout << endl << "The number of words entered is: " << n << endl; for (int i = 0; i < n; i++) cout << endl << words[i]; Note that there is still the constraint that there be no more than 50 words in this example. 12
Need for Arbitrary Sized Arrays The fixed size of arrays is a problem what if we want to do something as simple as read a set of integers and print them, but we have no clue how many integers there are? The following code breaks if there are more than 1000 integers entered: const int MAX = 1000; int a[max]; int j = 0; while (cin >> a[j]) j++ for (int i=0; i<j; i++) cout << a[i] << endl; How can we find a way around this problem? 13
Expanding an Array Solve the problem using dynamic allocation: int max = 10; int* a= new int[max]; int j = 0; while (cin >> a[j]) j++; if (j >= max) max = max*2; int *temp = new int[max]; for (int k=0; k<j; k++) temp[k] = a[k]; delete [] a; a = temp; for (int i=0; i<j; i++) cout << a[i] << endl; // make bigger array // copy into new array // free old array memory What can we say about the scope of the variable temp? 14