473,320 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Signal Handling in C++

san
Greetings,

I have a C++ problem. I am implementing a signal handler inside a class
SignalClass. The code looks as follows:
SignalClass is an Singleton class.

class SignalClass {
private:
int assign(int sig);
void handler (int sig);

};

void SignalClass::handler (int sig) {
cout << sig << endl;
}

int SignalClass::assign(int sig)
{
sigaction sa;
sa.sa_handler = handler; // ERROR at this line
.....
}

I am getting the compiler error as "Cannot assign
void(SignalClass::*)(int) to extern "C" void(*)(int)"

Any help in solving this problem is much appreciated.

Thanks in advance.

Feb 14 '06 #1
4 6493

"san" <sa**********@gmail.com> wrote in message

[snip]

| I have a C++ problem. I am implementing a signal handler inside a class
| SignalClass. The code looks as follows:
| SignalClass is an Singleton class.
| I am getting the compiler error as "Cannot assign
| void(SignalClass::*)(int) to extern "C" void(*)(int)"

Type of pointer to function is different from pointer to a member function,
hence the error.

| Any help in solving this problem is much appreciated.

Check this FAQ --
http://www.parashift.com/c++-faq-lit....html#faq-33.2

Sharad
Feb 14 '06 #2
san wrote:
Greetings,

I have a C++ problem. I am implementing a signal handler inside a class
SignalClass. The code looks as follows:
SignalClass is an Singleton class.

class SignalClass {
private:
int assign(int sig);
void handler (int sig);

};

void SignalClass::handler (int sig) {
cout << sig << endl;
}

int SignalClass::assign(int sig)
{
sigaction sa;
sa.sa_handler = handler; // ERROR at this line
....
}

I am getting the compiler error as "Cannot assign
void(SignalClass::*)(int) to extern "C" void(*)(int)"

Any help in solving this problem is much appreciated.

Thanks in advance.


What is sigaction? And how is its member sigaction::sa_handler declared?

If I guess right sigaction::sa_handler is a pointer to a function while
in the line of error handler is a (pointer to a) member function.
Consider declaring sigaction::sa_handler as

class sigaction{
public:
void (SignalClass::*(sa_handler))(int);
};

Or changing SignalClass::handler as:

class SignalClass{
static void handler(int);
};

Regards,
Ben
Feb 14 '06 #3
If these are signals on the C (or POSIX) sense, make sure that you
follow the C++ or POSIX restrictions, respectively, on signal handers.
In particular, a C++ signal must be a Plain Old Function (not a member
function), and use of std::cout (which is not of typt sigatomic_t) as a
non-const will cause its value to become undefined.

This link has more details:
http://www.thescripts.com/forum/thread138191.html

Feb 14 '06 #4
san wrote:
Greetings,

I have a C++ problem. I am implementing a signal handler inside a class
SignalClass. The code looks as follows:
SignalClass is an Singleton class.

class SignalClass {
private:
int assign(int sig);
void handler (int sig);

};

void SignalClass::handler (int sig) {
cout << sig << endl;
}

int SignalClass::assign(int sig)
{
sigaction sa;
sa.sa_handler = handler; // ERROR at this line
....
}

I am getting the compiler error as "Cannot assign
void(SignalClass::*)(int) to extern "C" void(*)(int)"

Any help in solving this problem is much appreciated.

Thanks in advance.


Signal Handler functions must be stand-alone 'C' functions
(i.e. not members of a class). If declared in a C++ file
they must be declared 'extern "C"'.

Larry
Feb 14 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Lionel van den Berg | last post by:
Hi all, I'm trying to do some signal handling using the csignal library but I can't find and specific examples for C++. The signal function as defined in C takes a parameter that is the signal...
2
by: lpw | last post by:
I have dilligently reviewed FAQ-lite Section 3.2, "How do I pass a pointer-to-member-function to a signal handler, X event callback, system call that starts a thread/task, etc." The only...
3
by: Martin McCormick | last post by:
A C program contains several signal statements to remove a lock file if the program gets killed: /*Set up interrupt handler to catch ctrl-C so that lock file can be removed.*/...
4
by: Eric Boutin | last post by:
Hi ! currently reading C docs, I think I'm reading docs about the stdc lib, but I'm not shure.. it talks about signals, does *all* OS implement the signal function, and use it well ? I mean.. I...
7
by: Stanley S | last post by:
Hi, Are Signal Handling part of ANSI C? I am not able to find any reference of Sig Handling in Stephen Prata's "C Primer Plus". The usage of signals is to trap errors I guess. (It looks...
10
by: subramanian | last post by:
Consider the following code: segment violation is deliberately generated to catch the SIGSEGV signal. The handler for this signal, namely SIGSEGV_handler, is called. However after the handler is...
2
by: hg | last post by:
Hi, I posted an equivalent question earlier ... but am still not sure: I currently (under Linux) have a program that uses Queue.put (raw_input('')) in a signal handler and Queue.get() in the...
3
by: vijay.db | last post by:
Hi Group, Running DB2 V8.2 Fxpack 9 in AIX 5.2, I get the following error frequently and my instance is stopped...collected some info like: the signal received is 11 which is SEGMENTATION...
1
by: 32Alpha | last post by:
Hi, first post here. First off, this IS a homework assignment for an operating systems class, but the question isn't "how do i do the assignment" but "why is my particular implementation not...
0
by: Marcin Krol | last post by:
Right, I didn't realize before that Python interpreter has its own signal handling routines. Now I am able to handle signals in Python code, but it still barfs on exit: import time import...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.