Function Pointers?

Started by
14 comments, last by pragma Fury 18 years, 10 months ago
Quote:Original post by Gink
So they are used for convenience and not because they are needed?


In the same way that C++ is used for convenience, and not because we can't do everything in assembly.

The thing is that they can accept ANY (appropriate) function, so unrelated bits of the program can interoperate better. For example, in the GLUT library they use callbacks a lot, which looks something like this:

//==VVV== Inside the GLUT Library ==VVV==typedef void (* f_void_void )( void );f_void_void display_function;void glutDisplayFunc( f_void_void function ) {    display_function = function;}void glutMainLoop ( void ) {    while ( 1 ) {        //...        display_function();        swap_buffers();    }}//==^^^== Inside the GLUT Library ==^^^==//==VVV== Inside your program ==VVV==#include <GL/glut.h>void render( void ) {    //...}int main ( void ) {    //...    glutDisplayFunc( render );    glutMainLoop();}


In order to have glutMainLoop allways call render() without function pointers, you'd have to edit the GLUT source code (which would be a pain in the butt) and require that each program have their own version of the GLUT library instead of sharing a single one.

Hope this helps to explain :-).
Advertisement
Quote:Original post by pragma Fury
I would somewhat agree with this. A lot of what you can do with function pointers can be done in an alternate way. Though that often results in some very nasty code and bad design practices.


Loading libraries at run-time (LoadLibrary/dlopen) and calling the functions they contain (GetProcAddress/dlsym) is an example of something that is literally impossible without function pointers (or the equivalent ASM trickery), since neither the library, nor the function are available at compile, link or load time.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
Quote:Original post by pragma Fury
I would somewhat agree with this. A lot of what you can do with function pointers can be done in an alternate way. Though that often results in some very nasty code and bad design practices.


Loading libraries at run-time (LoadLibrary/dlopen) and calling the functions they contain (GetProcAddress/dlsym) is an example of something that is literally impossible without function pointers (or the equivalent ASM trickery), since neither the library, nor the function are available at compile, link or load time.


True dat.
I havn't done much work with run-time libraries, so that totally skipped my mind.
If you want to use quicksort, you have to write your own function that does the following, and if so, why?

Quote:compare should return negative if the first argument is less than the second, zero if they are equal, and positive if the first argument is greater than the second
bump
The qsort function requires those return values, because that's what a quick-sort algorithm needs to function.

Here's a presentation on how a basic quicksort works.

Now, if I wanted to use qsort, and sort an array of integers in ascending order and then descending order using the same qsort algorithm, I'd write something like this:
int SortAscending(const void *element1, const void *element2){   int n1 = *reinterpret_cast<const int*>(element1);   int n2 = *reinterpret_cast<const int*>(element2);   if(n1 > n2)      return 1;   else if(n1 < n2)      return -1;   else      return 0;}int SortDescending(const void *element1, const void *element2){   int n1 = *reinterpret_cast<const int*>(element1);   int n2 = *reinterpret_cast<const int*>(element2);   if(n1 > n2)      return -1;   else if(n1 < n2)      return 1;   else      return 0;}int main(int argc, char* argv[]){      int aValues[10];   int nIndex;   // fill our array with 10 random values.   for(nIndex=0; nIndex<10; nIndex++)   {      aValues[nIndex] = rand()%100;   }   // print the unsorted array   printf("unsorted:      ");   for(nIndex=0; nIndex < 10; nIndex++)   {      printf("%2d%s",aValues[nIndex], nIndex==9?"\r\n":", ");   }   // sort ascending, passing in a pointer to my lowest->highest sorting function   qsort(aValues, 10, sizeof(int), &SortAscending);   // print the sorted array.   printf("sorted (up):   ");   for(nIndex=0; nIndex < 10; nIndex++)   {      printf("%2d%s",aValues[nIndex], nIndex==9?"\r\n":", ");   }   // sort descending, passing in a pointer to my highest->lowest sorting function   qsort(aValues, 10, sizeof(int), &SortDescending);   // print the sorted array.   printf("sorted (down): ");   for(nIndex=0; nIndex < 10; nIndex++)   {      printf("%2d%s",aValues[nIndex], nIndex==9?"\r\n":", ");   }   return 0;}


output:
unsorted:      41, 67, 34,  0, 69, 24, 78, 58, 62, 64sorted (up):    0, 24, 34, 41, 58, 62, 64, 67, 69, 78sorted (down): 78, 69, 67, 64, 62, 58, 41, 34, 24,  0

This topic is closed to new replies.

Advertisement