Problem with pointer to external function
Hello list,
why do it get the segfault below? What's wrong?
[sky@paktahn ~/code/cbtest]% cat main.c
#include <stdio.h>
typedef void (*callback)();
extern callback cb;
int
main (int argc, char** argv)
{
cb();
return(0);
}[sky@paktahn ~/code/cbtest]% cat defhandlers.c
void cb()
{
}
[sky@paktahn ~/code/cbtest]% gcc main.c defhandlers.c -ggdb
[sky@paktahn ~/code/cbtest]% gdb ./a.out
GNU gdb 6.3
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu"...Using host libthread_db library "/lib/tls/libthread_db.so.1".
(gdb) run
Starting program: /home/sky/code/cbtest/a.out
Program received signal SIGSEGV, Segmentation fault. 0x5de58955 in ?? ()
(gdb) bt full
#0 0x5de58955 in ?? ()
No symbol table info available.
#1 0x08048367 in main (argc=1, argv=0xbffff634) at main.c:10 No locals.
(gdb)
Regards,
Leslie
Signature · 1 line
PGP-KID: 0x52D70289
Hey,
I'd write it like that:
main.c
----
#include <stdio.h>
extern void cb();
void (*callback)()=cb;
int main (int argc, char** argv){
callback();
return(0);
}defhandler.c
----
void cb()
{
printf("handler\n");
}$ gcc main.c defhandler.c -o test $ ./test handler
Markus
Quoted 57 lines · On 7/1/05, [email protected] <[email protected]> wrote:
Hello list,
why do it get the segfault below? What's wrong?
[sky@paktahn ~/code/cbtest]% cat
main.c
#include <stdio.h>typedef void (*callback)();
extern callback cb;
int
main (int argc, char** argv){ cb(); return(0); }[sky@paktahn ~/code/cbtest]% cat
defhandlers.c
voidcb()
{
}[sky@paktahn ~/code/cbtest]% gcc
main.cdefhandlers.c-ggdb
[sky@paktahn ~/code/cbtest]% gdb ./a.out
GNU gdb 6.3
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i686-pc-linux-gnu"...Using hostlibthread_dblibrary "/lib/tls/libthread_db.so.1".(gdb) run
Starting program:/home/sky/code/cbtest/a.outProgram received signal SIGSEGV, Segmentation fault.
0x5de58955in ?? ()
(gdb) bt full
#00x5de58955in ?? ()
No symbol table info available.
#10x08048367in main (argc=1, argv=0xbffff634) at main.c:10 No locals.
(gdb)Regards,
Leslie
--
PGP-KID:0x52D70289
Quoted 1 line · On 7/1/05, [email protected] <[email protected]> wrote:
Hello list,
Hi Leslie
Quoted 1 line
why do it get the segfault below? What's wrong?
See below...
Quoted 8 lines
[sky@paktahn ~/code/cbtest]% cat
main.c
#include <stdio.h>typedef void (*callback)();
extern callback cb;
Quoted 8 lines
int
main (int argc, char** argv){ cb(); return(0); }
Your code can be corrected easily, Can you see the difference between the following two typedefs and their usage? Both versions work as expected:
#include <stdio.h>
typedef void callback();
typedef void (*callback2)();
extern callback cb;
extern callback cb2;
int main (int argc, char** argv)
{
cb();
(*cb2)();
return(0);
}You can simplify your code like this:
extern void cb();
void (*callback)() = cb;
Remember: function names are pointers. That's why the following two assignments are equally legal and do the same thing:
int myfunc(int a, int b) { return a + b; }
/* pointer to a function that takes two arguments */ int (*p)(int a, int b) = NULL;
p = myfunc; /* p points to myfunc now */
p = &myfunc; /* this way is recommended */
I usally encourage people not to combine typdefs and function pointers because they are often misleading and reduce readability of code. C has everything on board to implement and use callbacks without the need to define new types.
Kind Regards
\Steve
--
Steve Graegert <[email protected]> || <http://www.technologies.de/~sg/>
Independent Software Consultant {C/C++ && Java && .NET}
Mobile: +49 (176) 21 24 88 69
Office: +49 (9131) 71 26 40 9
Quoted 9 lines · [email protected] wrote:
why do it get the segfault below? What's wrong?
[sky@paktahn ~/code/cbtest]% cat
main.c
#include <stdio.h>typedef void (*callback)();
extern callback cb;
Is "cd" really a pointer to a function? Or is it a function? If it's the latter, you need to create a variable of type "callback" and then make it point to the function, e.g.:
typedef void (*callback)(); extern void cb(); static callback cbp = &cb;
More generally, you have to ensure that the types of "extern" declarations are correct. The compiler can't check it for you.
Signature · 1 line
Glynn Clements <[email protected]>