c - Core dumped when a function pointer is assigned with a funtion that has the same name in another file -
i abstract problem following scenario: 3 files:a.h,a.c,b.c,and code below:
a.c
#include "a.h" #include <stdio.h> int (*call2)(); int call1(int (*cb)()){ call2=cb; printf("success!"); return 1; }
a.h
int call1();
b.c
#include <stdio.h> #include "a.h" int call2(){return 0;}; int main(){ call1(call2); }
then compiling these files gcc a.c b.c -o b
warings:
/usr/bin/ld: warning: alignment 1 of symbol `call2' in /tmp/cc0wbcyh.o smaller 8 in /tmp/ccudjees.o /usr/bin/ld: warning: size of symbol `call2' changed 8 in /tmp/ccudjees.o 11 in /tmp/cc0wbcyh.o /usr/bin/ld: warning: type of symbol `call2' changed 1 2 in /tmp/cc0wbcyh.o
then run './b'
segmentation fault (core dumped)
my ideas: apparently, line call2=cb;
has caused corrupt.that means,assigning function function pointer has same name wrong operation.i think reason relates how gcc compiler store function pointer , function.but not familiar implementing of gcc compiler. can me?
by defining , call2
2 different objects, 2 different types, have violated 1 definition rule. behaviour undefined.
to achieve behaviour want, declare both call2
's static
have internal linkage , don't conflict each other.
Comments
Post a Comment