View Full Version : Division by zero
felix
09-21-2004, 03:09 PM
Hello,
I saw in a forum a programmer said that a division by zero can cause horrible problems at codes. Someone can explaim me what really happen, why happen, what happen ? :)
Regards,
i3839
09-21-2004, 07:39 PM
Your program will get a SIGFPE and if it doesn't handle it it will be killed. At least on my Linux pc. If you do trap SIGFPE then it will happen in a loop.
So no horrible problems, just a clean crash.
void fpe(){
static int j = 0;
printf("SIGFPE %i\n", ++j);
}
int main(int argc, char** argv){
signal(SIGFPE, fpe);
int i = 5 / atoi(argv[1]);
printf("%i\n", i);
return 0;
}
felix
09-21-2004, 10:05 PM
Hello,
Thks i3839. Much intersting. Why a infinite loop if i handle it ? It print number starting by one and goes incrementing to big numbers. Why ?
Could it not cause denial of service in systems that doesn't handle it ?
Regards,
i3839
09-21-2004, 10:24 PM
That's because of how divisions are done internally in the cpu. It only happens when you do catch the SIGFPE signal, just catching it isn't really handling it. If all you do is divide by zero then your program will be killed.
If your program doesn't prevent a division by zero, and doesn't want to be killed if it happens, then it's buggy.
felix
09-22-2004, 02:12 PM
Hello,
Thks once again i3839.
Regards,
vBulletin® v3.7.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.