5 11 Function Pointers Pointers to functions Contain
- Slides: 45
5. 11 Function Pointers • Pointers to functions – Contain address of function – Similar to how array name is address of first element – Function name is starting address of code that defines function • Function pointers can be – – Passed to functions Returned from functions Stored in arrays Assigned to other function pointers 2003 Prentice Hall, Inc. All rights reserved.
5. 11 Function Pointers • Calling functions using pointers – Assume parameter: • bool ( *compare ) ( int, int ) – Execute function with either • ( *compare ) ( int 1, int 2 ) – Dereference pointer to function to execute OR • compare( int 1, int 2 ) – Could be confusing • User may think compare name of actual function in program 2003 Prentice Hall, Inc. All rights reserved.
1 2 3 // Fig. 5. 25: fig 05_25. cpp // Multipurpose sorting program using function pointers. #include <iostream> 4 5 6 7 using std: : cout; using std: : cin; using std: : endl; 8 9 #include <iomanip> 10 11 using std: : setw; 12 13 14 15 16 17 // prototypes void bubble( int [], const int, bool (*)( int, int ) ); void swap( int * const, int * const ); bool ascending( int, int ); bool descending( int, int ); 18 19 20 21 22 23 24 int main() { const int array. Size = 10; int order; int counter; int a[ array. Size ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; Outline fig 05_25. cpp (1 of 5) Parameter is pointer to function that receives two integer parameters and returns bool result. 25 2003 Prentice Hall, Inc. All rights reserved.
26 27 28 29 30 31 32 33 cout << "Enter 1 to sort in ascending order, n" << "Enter 2 to sort in descending order: " ; cin >> order; cout << "n. Data items in original ordern" ; // output original array for ( counter = 0; counter < array. Size; counter++ ) cout << setw( 4 ) << a[ counter ]; 34 35 36 37 38 39 40 // sort array in ascending order; pass function ascending // as an argument to specify ascending sorting order if ( order == 1 ) { bubble( a, array. Size, ascending ); cout << "n. Data items in ascending ordern" ; } 41 42 43 44 45 46 47 // sort array in descending order; pass function descending // as an agrument to specify descending sorting order else { bubble( a, array. Size, descending ); cout << "n. Data items in descending ordern" ; } Outline fig 05_25. cpp (2 of 5) 48 2003 Prentice Hall, Inc. All rights reserved.
49 50 51 // output sorted array for ( counter = 0; counter < array. Size; counter++ ) cout << setw( 4 ) << a[ counter ]; 52 53 cout << endl; 54 55 return 0; fig 05_25. cpp (3 of 5) // indicates successful termination 56 57 } // end main 58 59 60 61 62 63 64 65 // multipurpose bubble sort; parameter compare is a pointer to functionorder that receives two // the comparison function that determines sorting integer parameters and returns void bubble( int work[], const int size, bool (*compare)( int, int ) ) bool result. { // loop to control passes Parentheses necessary to for ( int pass = 1; pass < size; pass++ ) 66 67 68 69 70 71 72 Outline compare is pointer to indicate pointer to function // loop to control number of comparisons per pass Call- passed function for ( int count = 0; count < size 1; count++ ) compare; dereference pointer execute function. out of to order, swap them // if adjacent elements are if ( (*compare)( work[ count ], work[ count + 1 ] ) ) swap( &work[ count ], &work[ count + 1 ] ); 2003 Prentice Hall, Inc. All rights reserved.
73 74 } // end function bubble 75 76 77 78 79 80 81 82 // swap values at memory locations to which // element 1 Ptr and element 2 Ptr point void swap( int * const element 1 Ptr, int * const element 2 Ptr ) { int hold = *element 1 Ptr; *element 1 Ptr = *element 2 Ptr; *element 2 Ptr = hold; 83 84 } // end function swap 85 86 87 88 89 90 91 92 Outline fig 05_25. cpp (4 of 5) // determine whether elements are out of order // for an ascending order sort bool ascending( int a, int b ) { return b < a; // swap if b is less than a } // end function ascending 93 2003 Prentice Hall, Inc. All rights reserved.
94 95 96 97 98 99 100 // determine whether elements are out of order // for a descending order sort bool descending( int a, int b ) { return b > a; // swap if b is greater than a } // end function descending Enter 1 to Enter 2 to Data items 2 6 Data items 2 4 fig 05_25. cpp (5 of 5) fig 05_25. cpp output (1 of 1) sort in ascending order, sort in descending order: 1 in original order 4 8 10 12 89 68 in ascending order 6 8 10 12 37 45 Outline 45 37 68 89 Enter 1 to sort in ascending order, Enter 2 to sort in descending order: 2 Data items in original order 2 6 4 8 10 12 89 68 Data items in descending order 89 68 45 37 12 10 8 6 45 37 4 2 2003 Prentice Hall, Inc. All rights reserved.
5. 11 Function Pointers • Arrays of pointers to functions – Menu driven systems – Pointers to each function stored in array of pointers to functions • All functions must have same return type and same parameter types – Menu choice subscript into array of function pointers 2003 Prentice Hall, Inc. All rights reserved.
1 2 3 // Fig. 5. 26: fig 05_26. cpp // Demonstrating an array of pointers to functions. #include <iostream> 4 5 6 7 using std: : cout; using std: : cin; using std: : endl; 8 9 10 11 12 // function prototypes void function 1( int ); void function 2( int ); void function 3( int ); 13 14 15 16 17 18 int main() Array initialized with names { of threethat functions; // initialize array of 3 pointers to functions each function // take an int argument and return void names are pointers. void (*f[ 3 ])( int ) = { function 1, function 2, function 3 }; 19 20 int choice; 21 22 23 cout << "Enter a number between 0 and 2, 3 to end: " ; cin >> choice; Outline fig 05_26. cpp (1 of 3) 24 2003 Prentice Hall, Inc. All rights reserved.
25 26 // process user's choice while ( choice >= 0 && choice < 3 ) { 27 28 29 30 // invoke function at location choice in array f // and pass choice as an argument (*f[ choice ])( choice ); 31 32 33 34 } 35 36 cout << "Program execution 37 38 return 0; Outline fig 05_26. cpp (2 of 3) cout << "Enter a number between 0 and 2, 3 to end: " ; cin >> choice; Call chosen function by dereferencing corresponding completed. " element<<inendl; array. // indicates successful termination 39 40 } // end main 41 42 43 44 45 void function 1( int a ) { cout << "You entered " << a << " so function 1 was callednn" ; 46 47 } // end function 1 48 2003 Prentice Hall, Inc. All rights reserved.
49 50 51 52 void function 2( int b ) { cout << "You entered " << b << " so function 2 was callednn" ; 53 54 } // end function 2 55 56 57 58 59 void function 3( int c ) { cout << "You entered " << c << " so function 3 was callednn" ; 60 61 } // end function 3 Outline fig 05_26. cpp (3 of 3) fig 05_26. cpp output (1 of 1) Enter a number between 0 and 2, 3 to end: 0 You entered 0 so function 1 was called Enter a number between 0 and 2, 3 to end: 1 You entered 1 so function 2 was called Enter a number between 0 and 2, 3 to end: 2 You entered 2 so function 3 was called Enter a number between 0 and 2, 3 to end: 3 Program execution completed. 2003 Prentice Hall, Inc. All rights reserved.
Memory It is the job of the Operating System to • Manage the allocation of memory to processes • Keep track of what memory/addresses each process is allowed to access • Reserve portion of memory for use by the OS • Enforce protection of memory space of each process 2003 Prentice Hall, Inc. All rights reserved.
• Pointer – a variable whose value is a memory address • Pointee – the value in memory whose address is stored in a pointer – the pointee is the target of the pointer. • A pointer may or may not have a target. Pointers without targets should be set to 0 or NULL.
int x = 4, y = 99; int *p 1, *p 2; p 1 = &x; p 2 = &y; *p 1 = 3; cout << *p 1 << “ “<< *p 2; p 2 = p 1; cout << *p 1 << “ “<< *p 2;
float i, x, *p, *q; p = &i; *p = 10. 0; q = &x; *q = 8. 6; cout << x << endl; cout << I << endl; *p = 9. 5; p = q; q = &i; cout << *p <<endl; cout << i <<endl; cout << x << endl; cout << *q << endl;
int *p, *q, *r, *s; int a, b, c, d[10]; p = &a; q = &b; *q = 98; *p = 76; q = p; cout <<*p << endl; cout << *q << endl; r = &d[0]; //note this is the same as saying r=d, which is also valid for (int i =0; i < 10; i++) r[i] = i; p = r; ++p; s= p; ++s; cout << *r << endl; cout << *q << endl; cout << *p <<endl; cout << *s <<endl;
Dynamic Allocation Using the new command: int *p 1 = new int; int *p 2 = new double[1000]; for (i = 0; i < 1000; i++) p 2[i] = 2*i; delete p 1; delete [] p 2;
int *p, *q, *r, *s; p = new int; *p = 76; cout <<*p << endl; r = new int [10]; //note this is the same as saying r=d, which is also valid for (int i =0; i < 10; i++) r[i] = i; q = r; ++q; s= q; ++s; cout << *r << endl; cout << *q << endl; cout << *p <<endl; cout << *s <<endl;
Memory Leak int *p 1 = new int; int *p 2 = new double[1000]; for (i = 0; i < 1000; i++) p 2[i] = 2*i; p 2 = p 1; //NOW THE ARRAY IS LOST delete p 1; delete [] p 2;
Array Resizing
5. 12. 1 Fundamentals of Characters and Strings • Character constant – Integer value represented as character in single quotes – 'z' is integer value of z • 122 in ASCII • String – Series of characters treated as single unit – Can include letters, digits, special characters +, -, *. . . – String literal (string constants) • Enclosed in double quotes, for example: "I like C++" – Array of characters, ends with null character '