Pointer Casting warning
-
Hi, while converting C code to C++, I get a casting warning that I can't get rid of "Warning (Anachronism): Assigning void(*)(int) to extern "C" void(*)(int)" Anyone? Thanks
#include struct sigaction sgnl_action;
void handle_alarm() {
alarm_timededout_=TRUE;
...
}
//In C no warnings
void alarm_manager_(char *typ) {
if (!strcmp(typ,"init")) {
void (*func)() = handle_alarm;
sgnl_action.sa_handler = func;
....
}//In C++
void alarm_manager_(char *typ) {
if (!strcmp(typ,"init")) {
sgnl_action.sa_handler = (void (*)(int))handle_alarm; /////warning on this line
....
} -
Hi, while converting C code to C++, I get a casting warning that I can't get rid of "Warning (Anachronism): Assigning void(*)(int) to extern "C" void(*)(int)" Anyone? Thanks
#include struct sigaction sgnl_action;
void handle_alarm() {
alarm_timededout_=TRUE;
...
}
//In C no warnings
void alarm_manager_(char *typ) {
if (!strcmp(typ,"init")) {
void (*func)() = handle_alarm;
sgnl_action.sa_handler = func;
....
}//In C++
void alarm_manager_(char *typ) {
if (!strcmp(typ,"init")) {
sgnl_action.sa_handler = (void (*)(int))handle_alarm; /////warning on this line
....
}What happens with:
void handle_alarm( int )
{
}
...
sgnl_action.sa_handler = handle_alarm; -
What happens with:
void handle_alarm( int )
{
}
...
sgnl_action.sa_handler = handle_alarm;I get the same exact warning, thats how I had it originally, then I explicitly specified the casting thinking it will get rid of the warning. It seems to get rid of the warning if I do this, but not sure if its correct functionality?
extern "C" void print(char *)
{
...
} -
Hi, while converting C code to C++, I get a casting warning that I can't get rid of "Warning (Anachronism): Assigning void(*)(int) to extern "C" void(*)(int)" Anyone? Thanks
#include struct sigaction sgnl_action;
void handle_alarm() {
alarm_timededout_=TRUE;
...
}
//In C no warnings
void alarm_manager_(char *typ) {
if (!strcmp(typ,"init")) {
void (*func)() = handle_alarm;
sgnl_action.sa_handler = func;
....
}//In C++
void alarm_manager_(char *typ) {
if (!strcmp(typ,"init")) {
sgnl_action.sa_handler = (void (*)(int))handle_alarm; /////warning on this line
....
}The parentheses you have in
sgnl_action.sa_handler = (void (*)(int))handle_alarm; /////warning on this line
looks strange to me. Perhaps
sgnl_action.sa_handler = (void*)((int)handle_alarm); /////warning on this line
would solve your problem. Just a guess at the moment - not near my VS to test it.
Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
The parentheses you have in
sgnl_action.sa_handler = (void (*)(int))handle_alarm; /////warning on this line
looks strange to me. Perhaps
sgnl_action.sa_handler = (void*)((int)handle_alarm); /////warning on this line
would solve your problem. Just a guess at the moment - not near my VS to test it.
Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
This would give an error because the cast is (void(*)(int))(func). Anyways, the complain is about tryiing to cast to an extern "C", when I change my function to an extern "C", it seems to work fine. So, it looks as the signal handler functions have to be standalone external C functions. Thanks
-
Hi, while converting C code to C++, I get a casting warning that I can't get rid of "Warning (Anachronism): Assigning void(*)(int) to extern "C" void(*)(int)" Anyone? Thanks
#include struct sigaction sgnl_action;
void handle_alarm() {
alarm_timededout_=TRUE;
...
}
//In C no warnings
void alarm_manager_(char *typ) {
if (!strcmp(typ,"init")) {
void (*func)() = handle_alarm;
sgnl_action.sa_handler = func;
....
}//In C++
void alarm_manager_(char *typ) {
if (!strcmp(typ,"init")) {
sgnl_action.sa_handler = (void (*)(int))handle_alarm; /////warning on this line
....
}Because the unix / posix libraries work with either C or C++, they all declare their external functions as 'extern "C"' to get the C calling conventions. This is done in the header files, in your case, Signal.h
#ifdef __cplusplus
extern "C" {
#endifThe field sa_handler needs to point to a function that is declared as "C" callable. Use __cdecl. Note that C calling conventions also affect how the stack arguments are cleaned up so unless you declare the action function correctly, you will end up with stack corruption problems later.
void __cdecl handle_alarm() {
alarm_timededout_=TRUE;
...
} -
Hi, while converting C code to C++, I get a casting warning that I can't get rid of "Warning (Anachronism): Assigning void(*)(int) to extern "C" void(*)(int)" Anyone? Thanks
#include struct sigaction sgnl_action;
void handle_alarm() {
alarm_timededout_=TRUE;
...
}
//In C no warnings
void alarm_manager_(char *typ) {
if (!strcmp(typ,"init")) {
void (*func)() = handle_alarm;
sgnl_action.sa_handler = func;
....
}//In C++
void alarm_manager_(char *typ) {
if (!strcmp(typ,"init")) {
sgnl_action.sa_handler = (void (*)(int))handle_alarm; /////warning on this line
....
}hi I dnt knw clearly what type of
sgnl_action.sa_handler
is. But this is a little piece of code without any warning and errors might be helpful to you.
#include <stdio.h>
void (*sa_handler)(int); // function pointer takes int type
// arg and retuns void as we have to
//typecast func handle_alarm to this typevoid handle_alarm() {
printf("helooo");
}void main(int argc, _TCHAR* argv[])
{
handle_alarm(); //normal call to function
sa_handler = (void (*)(int))handle_alarm; // compatible typecast
sa_handler(0); // call with a function pointer
// as it takes one argument we have
//to pass a dummy arg.
}