gnu.gcc.help
[Top] [All Lists]

Re: Catching SIGFPE two times

Subject: Re: Catching SIGFPE two times
From: thomas.mertes@xxxxxx
Date: Wed, 9 Jul 2008 23:51:40 -0700 (PDT)
Newsgroups: gnu.gcc.help

On 10 Jul., 00:17, thomas.mer...@xxxxxx wrote:
> Hello
>
> I have problems when I try to catch a division by zero for
> two times. I try to emulate exceptions with setjmp/longjmp.
>
> To catch a division by zero I use a handler for the signal
> SIGFPE. In the signal handler I use longjmp to jump to the
> exception handler.
>
> What happens is: The first division by zero is handled
> correctly but the second one terminates the program.
> To demonstrate this, use the following test program:
> --------------------------------------------------
> #include "stdio.h"
> #include "setjmp.h"
>
> #define SIGFPE 8
> #define SIG_IGN 1
>
> jmp_buf catch_pos;
>
> static void handle_fpe_signal (int sig_num)
>   {
>     signal(SIGFPE, handle_fpe_signal);
>     printf("in handle_fpe_signal catch_pos=%lx\n", catch_pos);
>     longjmp(catch_pos, 1);
>   }
>
> int main()
>   {
>     int fail_value;
>     int x;
>
>     signal(SIGFPE, handle_fpe_signal);
>     if ((fail_value = setjmp(catch_pos)) == 0) {
>       printf("before division A catch_pos=%lx\n", catch_pos);
>       x = 42/0;
>       printf("after division A\n");
>     } else {
>       printf("handler A\n");
>     }
>     printf("---------- A\n");
>
>     if ((fail_value = setjmp(catch_pos)) == 0) {
>       printf("before division B catch_pos=%lx\n", catch_pos);
>       x = 42/0;
>       printf("after division B\n");
>     } else {
>       printf("handler B\n");
>     }
>     printf("---------- B\n");
>
>     return 0; /* Never reached */
>   }
> ---------------------------------------------------
> Compiling and execution this test program (tst3.c) gives:
>
> tm@penguin$ cc tst3.c
> tst3.c: In Funktion »main«:
> tst3.c:24: Warnung: Division durch Null
> tst3.c:33: Warnung: Division durch Null
> tm@penguin$ ./a.out
> before division A catch_pos=8049860
> in handle_fpe_signal catch_pos=8049860
> handler A
> ---------- A
> before division B catch_pos=8049860
> Floating point exception
> tm@penguin$
>
> Has someone an idea how a program can have more than
> one SIGFPE catched with a setjmp/longjmp exception?
>
> Greetings Thomas Mertes
>
> Seed7 Homepage:  http://seed7.sourceforge.net
> Seed7 - The extensible programming language: User defined statements
> and operators, abstract data types, templates without special
> syntax, OO with interfaces and multiple dispatch, statically typed,
> interpreted or compiled, portable, runs under linux/unix/windows.

I forgot to mention;
$ gcc --version
gcc (GCC) 4.2.3 (Ubuntu 4.2.3-2ubuntu7)

$ uname -a
Linux penguin 2.6.24-19-386 #1 Wed Jun 18 14:09:56 UTC 2008 i686 GNU/
Linux

Greetings Thomas Mertes

Seed7 Homepage:  http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.

<Prev in Thread] Current Thread [Next in Thread>