Tirgul 5 Agenda Generic Programming in C Why

  • Slides: 17
Download presentation
Tirgul 5 - Agenda �Generic Programming in C �Why �void* �Pointers to functions �Promo

Tirgul 5 - Agenda �Generic Programming in C �Why �void* �Pointers to functions �Promo to CPP 1

Motivation �cretters. c example 2

Motivation �cretters. c example 2

Generic Programming �The goal: To write code once that works on a variety of

Generic Programming �The goal: To write code once that works on a variety of types �The tools in C: �pointers to functions �void* �The tools in C++: �polymorphism (not called Generic programming) �templates (“pure” Generic programming) �overloading 3

void* � A way to pass data of an arbitrary type � void* is

void* � A way to pass data of an arbitrary type � void* is a generic pointer capable of representing any pointer type int double void* p= &i; p= &f; i= 5; f= 3. 14; p; // ok � We’ve met void* before – where? malloc, string. h 4

“Rules” of void* �a void* pointer cannot be dereferenced int i= 5; double f=

“Rules” of void* �a void* pointer cannot be dereferenced int i= 5; double f= 3. 14; void* p; p= &i; p= &f; // ok printf("%fn", *p); // compilation error �Why? Because we don’t know what is there, thus we don’t know how to read it! 5

“Rules” of void* �void* can be explicitly cast to another pointer type int i=

“Rules” of void* �void* can be explicitly cast to another pointer type int i= 5; double f= 3. 14; void* p; p= &i; p= &f; // ok printf("%fn", *((double*)p)); // ok 6

Example �See code: swap_e. c 7

Example �See code: swap_e. c 7

Output �Make sure you understand parr 0, 1 new values 8

Output �Make sure you understand parr 0, 1 new values 8

Pointers to Functions �Are you serious? ! �Yes! �A friendly tutorial: http: //www. newty.

Pointers to Functions �Are you serious? ! �Yes! �A friendly tutorial: http: //www. newty. de/fpt/index. html �Assuming that function f is defined: &f and f are pointers to the function �i. e. : The address where the function's definition begins in memory 9

Example int avg(int num 1, int num 2) { return ( num 1 +

Example int avg(int num 1, int num 2) { return ( num 1 + num 2 ) / 2; } int (* func)(int, int); // a ptr variable func = &avg; func = avg; // assignment // same int result = avg(20, 30); result = (*func)(20, 30); // invoke it using the ptr result = func(20, 30); // same 10

Recommendation �Use typedef int (* Two. Ints. Func) (int, int); Two. Ints. Func f

Recommendation �Use typedef int (* Two. Ints. Func) (int, int); Two. Ints. Func f 1; f 1 = &avg; . . . f 1 = ∑ 11

What is it good for? �Generic programming - pass a function name to another

What is it good for? �Generic programming - pass a function name to another function as a parameter �Once upon a time… it was a way to make a c struct kind of a poor class 12

Classical Example - qsort #include <stdlib. h> Array to be sorted Number of elements

Classical Example - qsort #include <stdlib. h> Array to be sorted Number of elements in array sizeof of each element in array void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); Pointer to the comparison function Returns an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second 13

�See code qsort_e. c 14

�See code qsort_e. c 14

Output 15

Output 15

Muppets! �See code of critter. c �Note call to bsearch �Note call to qsort

Muppets! �See code of critter. c �Note call to bsearch �Note call to qsort 16

qsort problems or C++ “promo” �Not efficient – casting, calls to functions �Not user

qsort problems or C++ “promo” �Not efficient – casting, calls to functions �Not user freindly �Type safety problems �C++ improves all this problems 17