return from signal

Discussion in 'C++' started by Ahmed Ossman, Jul 22, 2003.

  1. Ahmed Ossman

    Ahmed Ossman Guest

    hi,
    I want to save the return value from the signal() function, what is the type
    of the variable.
    i.e. I want to do the following:
    <variable type?> ret_sig = signal(......);

    I need it soo much.
    Thanks,
    Ahmed Ossman
     
    Ahmed Ossman, Jul 22, 2003
    #1
    1. Advertising

  2. On Tue, 22 Jul 2003 14:11:48 +0300, "Ahmed Ossman" <> wrote:

    >I want to save the return value from the signal() function,


    At your current level, absolutely don't use signal(). It's
    incompatible with many C++ features.



    >what is the type of the variable.
    >i.e. I want to do the following:
    ><variable type?> ret_sig = signal(......);


    The result type is specified in the function declaration.



    >I need it soo much.


    You don't. If you can explain what you're trying to _achieve_
    by using signal(), perhaps we can help you.
     
    Alf P. Steinbach, Jul 22, 2003
    #2
    1. Advertising

  3. Ahmed Ossman

    Ahmed Ossman Guest

    The function is as following

    ***********************
    const char *dump_float(
    const float &real // reference to float to be output
    )
    // PUBLIC FUNCTION: dump_float() - format a float for dump.
    // This function returns a pointer to a 0-terminated character
    // vector containing the value of the float "real" formatted for
    // use in an object dump. It handles the case(s) in which the data
    // does not have a valid floating point representation. Such a
    // situation can arise when a union field of more than one data type
    // is interpreted as a float.
    {
    // save the float in a global so that the signal handler can output
    // the value if signal occurs.
    u_val.p_val = *((void**)(&real));
    // GNU strstream ctor does not take a mode
    #ifdef __GNUC__
    strstream out(ptr_buf, buf_size);
    #else
    strstream out(ptr_buf, buf_size, ios::eek:ut);
    #endif
    out_p = &out;
    // set up the handler for the SIGFPE signal (if appropriate)
    sig_handle_func_p prev_sigfpe_val = (sig_handle_func_p) signal(SIGFPE,
    SIG_IGN);
    if (prev_sigfpe_val != (sig_handle_func_p)SIG_IGN) {
    // install own signal handler
    signal(SIGFPE, sigfpe_handler);
    }
    // dump the floating point representation of the scalar value
    if (setjmp(sjbuf) == 0) {
    sprintf(pbuf, "%-15g", (double)real);
    out << pbuf;
    }
    if (prev_sigfpe_val != (sig_handle_func_p)SIG_IGN) {
    // restore the original action in the event of the SIGFPE signal.
    signal(SIGFPE, prev_sigfpe_val);
    }
    return ptr_buf;
    }
    *********************************************
    It is compiled with the forte 6 Solaries compiler, and the error is: Error:
    Could not find a macth for std::signal(int, void(*)(int))
    Could anyone help???
    Ahmed Ossman

    "Alf P. Steinbach" <> wrote in message
    news:...
    > On Tue, 22 Jul 2003 14:11:48 +0300, "Ahmed Ossman"

    <> wrote:
    >
    > >I want to save the return value from the signal() function,

    >
    > At your current level, absolutely don't use signal(). It's
    > incompatible with many C++ features.
    >
    >
    >
    > >what is the type of the variable.
    > >i.e. I want to do the following:
    > ><variable type?> ret_sig = signal(......);

    >
    > The result type is specified in the function declaration.
    >
    >
    >
    > >I need it soo much.

    >
    > You don't. If you can explain what you're trying to _achieve_
    > by using signal(), perhaps we can help you.
    >
    >
     
    Ahmed Ossman, Jul 22, 2003
    #3
  4. Don't top post, that's rude.



    On Tue, 22 Jul 2003 14:50:20 +0300, "Ahmed Ossman" <> wrote:

    >The function is as following


    OK, I'll first discuss the particulars of that function, then
    show how you can achieve a dump of a float value.



    >***********************
    >const char *dump_float(
    >const float &real // reference to float to be output
    >)


    Make the return value a std::string.

    You're returning a pointer to a global or perhaps a member
    character array.

    It's very evil.



    >// PUBLIC FUNCTION: dump_float() - format a float for dump.
    >// This function returns a pointer to a 0-terminated character
    >// vector containing the value of the float "real" formatted for
    >// use in an object dump. It handles the case(s) in which the data
    >// does not have a valid floating point representation. Such a
    >// situation can arise when a union field of more than one data type
    >// is interpreted as a float.


    Don't use unions, they're evil.

    Instead, use C++ virtual functions and inheritance.

    See the C++ FAQ.


    >{
    >// save the float in a global so that the signal handler can output
    >// the value if signal occurs.
    >u_val.p_val = *((void**)(&real));


    Now _that_ is unbelievably evil. How does the signal handler
    (note that a signal handler is also evil in C++) know that this
    void* pointer is a float*? And if it must always be, why not
    use a float*?

    Btw., globals like u_val are evil.


    >// GNU strstream ctor does not take a mode
    >#ifdef __GNUC__
    >strstream out(ptr_buf, buf_size);
    >#else
    >strstream out(ptr_buf, buf_size, ios::eek:ut);
    >#endif


    Use std::stringstream.


    >out_p = &out;


    Globals like out_p are evil.

    Use local variables, not globals.



    >// set up the handler for the SIGFPE signal (if appropriate)
    >sig_handle_func_p prev_sigfpe_val = (sig_handle_func_p) signal(SIGFPE,
    >SIG_IGN);


    You do not show the definition of sig_handle_func_p, but the cast
    indicates that it's an incorrect definition.



    >if (prev_sigfpe_val != (sig_handle_func_p)SIG_IGN) {
    >// install own signal handler
    >signal(SIGFPE, sigfpe_handler);


    This is probably where the compiler says "enough". You do not show
    your definition of sigfpe_handler. But judging from the compiler's
    error message it's something like


    void f( int x ) { ... }


    which is incorrect.



    >}
    >// dump the floating point representation of the scalar value
    >if (setjmp(sjbuf) == 0) {
    >sprintf(pbuf, "%-15g", (double)real);


    The use of sprintf is inconsistent with the use of strstream.

    At your current level, use std::stringstr.


    >out << pbuf;
    >}



    Here you simply ignore the case of a signal occurring, and
    proceed to return a pointer to an uninitialized buffer. That
    is a bug.



    >if (prev_sigfpe_val != (sig_handle_func_p)SIG_IGN) {
    >// restore the original action in the event of the SIGFPE signal.
    >signal(SIGFPE, prev_sigfpe_val);
    >}
    >return ptr_buf;
    >}
    >*********************************************
    >It is compiled with the forte 6 Solaries compiler, and the error is: Error:
    >Could not find a macth for std::signal(int, void(*)(int))




    Okay, what you're trying to achieve seems to be to convert a
    float value to decimal representation, if and only if it is a
    valid float value.

    The conversion part is easily accomplished in C++, and I suggest
    you write that as a simple function that _assumes_ (has as
    precondition) that the float value is valid. std::stringstr
    handles the cases of IEEE Inf and so on. The function declaration
    would be something like


    std::string toString( float aFloat )
    {
    // stuff here using std::strstream
    }


    The if and only if valid part is not easily accomplished in C++,
    since C++ allows the compiler to choose the representation of
    float and double. Here I suggest that you restrict your program
    to compilers that support IEEE standard format. You can check
    whether a particular compiler does via std::numeric_limits.


    Hth.,

    - Alf
     
    Alf P. Steinbach, Jul 22, 2003
    #4
  5. On Tue, 22 Jul 2003 12:46:14 GMT, (Alf P. Steinbach) wrote:

    > // stuff here using std::strstream


    Typo: that should be std::stringstream.
     
    Alf P. Steinbach, Jul 22, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Pronath
    Replies:
    1
    Views:
    1,210
    Diez B. Roggisch
    Jan 3, 2005
  2. Jack Orenstein

    threading.Thread vs. signal.signal

    Jack Orenstein, Sep 18, 2005, in forum: Python
    Replies:
    0
    Views:
    489
    Jack Orenstein
    Sep 18, 2005
  3. Weng Tianxiang
    Replies:
    2
    Views:
    678
    Jonathan Bromley
    Jan 30, 2007
  4. Nicolas Moreau
    Replies:
    9
    Views:
    3,300
  5. dibacco73
    Replies:
    1
    Views:
    680
    joris
    Feb 12, 2009
Loading...

Share This Page