* Problem with pointer to external function
@ 2005-07-01 10:30 leslie.polzer
2005-07-01 10:54 ` Rechberger Markus
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: leslie.polzer @ 2005-07-01 10:30 UTC (permalink / raw)
To: linux-c-programming; +Cc: leslie.polzer
[-- Attachment #1: Type: text/plain, Size: 1244 bytes --]
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
--
PGP-KID: 0x52D70289
[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Problem with pointer to external function
2005-07-01 10:30 Problem with pointer to external function leslie.polzer
@ 2005-07-01 10:54 ` Rechberger Markus
2005-07-01 11:17 ` Steve Graegert
2005-07-01 16:41 ` Glynn Clements
2 siblings, 0 replies; 4+ messages in thread
From: Rechberger Markus @ 2005-07-01 10:54 UTC (permalink / raw)
To: linux-c-programming
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
On 7/1/05, leslie.polzer@gmx.net <leslie.polzer@gmx.net> 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
> 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
>
> --
> PGP-KID: 0x52D70289
>
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Problem with pointer to external function
2005-07-01 10:30 Problem with pointer to external function leslie.polzer
2005-07-01 10:54 ` Rechberger Markus
@ 2005-07-01 11:17 ` Steve Graegert
2005-07-01 16:41 ` Glynn Clements
2 siblings, 0 replies; 4+ messages in thread
From: Steve Graegert @ 2005-07-01 11:17 UTC (permalink / raw)
To: leslie.polzer@gmx.net; +Cc: linux-c-programming
On 7/1/05, leslie.polzer@gmx.net <leslie.polzer@gmx.net> wrote:
> Hello list,
Hi Leslie
> why do it get the segfault below? What's wrong?
See below...
>
> [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);
> }
>
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 <graegerts@gmail.com> || <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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Problem with pointer to external function
2005-07-01 10:30 Problem with pointer to external function leslie.polzer
2005-07-01 10:54 ` Rechberger Markus
2005-07-01 11:17 ` Steve Graegert
@ 2005-07-01 16:41 ` Glynn Clements
2 siblings, 0 replies; 4+ messages in thread
From: Glynn Clements @ 2005-07-01 16:41 UTC (permalink / raw)
To: leslie.polzer; +Cc: linux-c-programming
leslie.polzer@gmx.net 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.
--
Glynn Clements <glynn@gclements.plus.com>
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2005-07-01 16:41 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-07-01 10:30 Problem with pointer to external function leslie.polzer
2005-07-01 10:54 ` Rechberger Markus
2005-07-01 11:17 ` Steve Graegert
2005-07-01 16:41 ` Glynn Clements
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).