announcement
play

Announcement Final exam Function Pointers Tuesday, May 20 th - PDF document

Announcement Final exam Function Pointers Tuesday, May 20 th 2:45 4:45pm Rm 70-1620 Announcement Speaking of Exams Pizza on Bill! Hope to have exam 2 graded by Thursday Visual Studio .NET Spring Tour When


  1. Announcement • Final exam Function Pointers – Tuesday, May 20 th – 2:45 – 4:45pm – Rm 70-1620 Announcement Speaking of Exams • Pizza on Bill! Hope to have exam 2 graded by Thursday • Visual Studio .NET Spring Tour – When • Wednesday, May 14 at 7:30 p.m. – Where • Webb Auditorium (Building 7A) – RSVP: • http://msdnaa.net/vsspringtour Projects Projects File Image • Drawing Image – Shapewin may not implement zooming Should all have received e-mail correctly! purify – Beware of memory leaks! Fractal Image – Due: May 12 th Start grading this week – Also due May 12 th – Extra implementation Send color file if you implemented this

  2. Projects Projects • Doing extra extras! • Using purify – Sure, but maximum grade is 105. – In makefile (or header.mak) – CCC = purify CC Plan for this week Recall: Program Memory • Function pointers • The memory used by a program is generally separated into the following sections: • Writing inserters and extractors – Code – Where the executable code is kept • Return Exam 2 – Global – Where storage for global variables is kept – Stack – Runtime stack (where local variables are kept) – Heap – Free store for dynamically allocated variables. – Exception – special place for things thrown Function pointers Function pointers: but why? • Provides access to executable code section. // the four arithmetic operations // one of these functions is selected at runtime • Function Pointers are pointers // with a switch or a function pointer – variables, which point to the address of a float Plus ( float a, float b) { return a+b; } function. float Minus ( float a, float b) { return a-b; } float Multiply ( float a, float b) { return a*b; } – Contains a memory address float Divide ( float a, float b) { return a/b; } • Examples from – http://www.function-pointer.org/ – Yes, function pointers have their own web site

  3. Function pointers: but why? Using function pointers // solution with a switch-statement – // solution with a function pointer // <opCode> specifies which operation to execute // <pt2Func> is a function pointer and points to // a function which takes two floats and returns a void Switch( float a, float b, char opCode) // float. Thefunction pointer { // "specifies" which operation shall be executed. float result; // execute operation switch (opCode) { void Switch_With_Function_Pointer( float a, float b, float (*pt2Func)( float , float )) case '+' : result = Plus (a, b); break ; { case '-' : result = Minus (a, b); break ; // call using function pointer case '*' : result = Multiply (a, b); break ; float result = pt2Func(a, b); case '/' : result = Divide (a, b); break ; cout << result << endl; } } cout << "switch: 2+5=" << result << endl; } Using function pointers Using function pointers // execute example code • Important note: – A function pointer always points to a function void Replace_A_Switch() with a specific signature! { // '+' specifies function 'Plus' to be executed – all functions, you want to use with the same Switch(2, 5, '+'); function pointer, must have the same parameters and return-type! //pointer to function 'Minus Switch_With_Function_Pointer(2, 5, &Minus); • Questions so far? } Function pointer syntax Function pointer syntax • int (*pt2Function) (float, char, char) ; • int (*pt2Function) (float, char, char) ; – Defines a pointer variable pt2Function – The function that this pointer is pointing to takes a float and 2 chars as arguments – The function that this pointer is pointing to will Name of variable Argument types return an int. Return holding the memory type address of a function

  4. Function pointer syntax Function pointer syntax • Note: • Note: – int (*pt2Function) (float, char, char) ; – int (*pt2Function) (float, char, char) ; • Defines a function pointer variable • Is not the same as – int *pt2Function (float, char, char) ; – int *pt2Function (float, char, char) ; • Defines a function that returns a pointer to an int. Function pointer syntax Function pointer syntax • Assigning to a function pointer: • Can also assign to member functions. int DoIt ( float a, char b, char c) class TMyClass { { printf("DoIt\n"); return a+b+c; } public : int DoIt ( float a, char b, char c){ return a+b+c; int DoMore( float a, char b, char c) }; int DoMore( float a, char b, char c){ return a- { printf("DoMore\n"); return a-b+c; } b+c; }; /* more of TMyClass */ }; int (*pt2Function) (float, char, char) ; pt2Function = DoMore; // assignment pt2Function = &DoIt; // alternative int (*pt2Function) (float, char, char) ; pt2Function = TMyClass::DoMore; // assignment Must have same arguments and return type! pt2Function = &TMyClass::DoIt; // alternative Function pointer syntax Function pointer syntax • Once again, return type and args must match: • Calling a Function using a Function Pointer void (*pf)(string); void f1 (string); – Can call directly or dereference int f2 (string); void f3 (int *); void f() int result1 = pt2Function (12, 'a', 'b'); { int result2 = (*pt2Function) (12, 'a', 'b'); pf = &f1; // okay pf = &f2; // bad return type pf = &f3; // bad arg type pf (“Foo”); // okay pf (1); // bad arg type int i = pf (“Zero”); // bad return type; }

  5. Function pointer syntax Function pointer syntax • Passing function pointer to a function • Returning a function pointer // function takes a char and returns a pointer to a // <pt2Func> is a pointer to a function which returns an int // function which is taking two and takes a float and two char void PassPtr( int (*pt2Func)( float , char , char )) // floats and returns a float. { // <opCode> specifies which function to return // call using function pointer float result = pt2Func(12, 'a', 'b'); float (*GetPtr1( const char opCode))( float , float ) } { // execute example code - 'DoIt' is a suitable function if (opCode == '+') return &Plus; void Pass_A_Function_Pointer() if (opCode == '-') return &Minus; { PassPtr(&DoIt); } } Function pointer syntax Function pointer syntax • float (*GetPtr1( const char opCode))( float , • Returning a function pointer float ) • Use typedef to avoid that crazy syntax Actual function name typedef float (*pt2Func)( float , float ); pt2Func GetPtr2( const char opCode) Indicates pointer Args of { to a function is function if (opCode == '+') return &Plus; returned pointed to if (opCode == '-') return &Minus; } Type returned by function pointed to Arrays of function pointers Arrays of function pointers • Since function pointers are just pointers, you can • But why? easily have arrays of them – Let’s assume we have a menu system for a GUI. typedef int (*pt2Function)( float , char , char ); / – Each menu item will correspond to an action. void Array_Of_Function_Pointers() { – Can use array of function pointers rather than a pt2Function funcArr[10]; large switch or if/then statements. funcArr[0] = &DoIt; funcArr[1] = &DoMore; printf("%d\n", funcArr[1](12, 'a', 'b')); printf("%d\n", funcArr[0](12, 'a', 'b')); }

  6. Arrays of function pointers Callbacks typedef void (*MenuF)(); • Function Pointers provide the concept of callback functions. MenuF edit_ops[] = { &cut, &copy, &paste, &find }; MenuF file_ops[] = { &open, &new, &close, &save }; • Example MenuF *button2 = edit_ops; MenuF *button3 = file_ops; typedef void (*terminate_handler)(); terminate_handler set_terminate(terminate_handler); // When selection is made Button2[2](); Questions? Callbacks Callbacks void qsort( ... , int (_USERENTRY *cmpFunc)( const void *, • Consider qsort: const void *)) { /* sort algorithm - note: item1 and item2 are void- void qsort( pointers */ void * field, int bigger=cmpFunc(item1, item2); // make callback size_t nElements, size_t sizeOfAnElement, /* use the result */ int (_USERENTRY *cmpFunc)( const void *, const void *) } ); Callback that defines compare function Callbacks • Of course, if we want to do generic programming, why not use STL? – And functors!! – But more on that tomorrow. – Questions?

Recommend


More recommend