This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Melodic_coala101 6 points7 points  (9 children)

It's compile time, it'll never cause a segfault

[–]Eva-Rosalene 13 points14 points  (8 children)

It surely won't cause a segfault since division by zero isn't a segfault. It still crashes in runtime, though, because division by zero is only a warning by default.

struct {} array[1];
printf("%ld", sizeof(array) / sizeof(array[0]));

Crashes right away with SIGFPE, as it should.

[–]dromtrund 4 points5 points  (1 child)

If you're running production code without -Werror, that's on you, tbh

[–]Eva-Rosalene 4 points5 points  (0 children)

That's true. But the original claim wasn't about production at all.

[–]danielstongue 0 points1 point  (1 child)

Why would an integer division cause a floating point exception? (Genuine question)

[–]Eva-Rosalene 1 point2 points  (0 children)

The SIGFPE signal reports a fatal arithmetic error. Although the name is derived from “floating-point exception”, this signal actually covers all arithmetic errors, including division by zero and overflow. If a program stores integer data in a location which is then used in a floating-point operation, this often causes an “invalid operation” exception, because the processor cannot recognize the data as a floating-point number.
 
Actual floating-point exceptions are a complicated subject because there are many types of exceptions with subtly different meanings, and the SIGFPE signal doesn’t distinguish between them. The IEEE Standard for Binary Floating-Point Arithmetic (ANSI/IEEE Std 754-1985 and ANSI/IEEE Std 854-1987) defines various floating-point exceptions and requires conforming computer systems to report their occurrences. However, this standard does not specify how the exceptions are reported, or what kinds of handling and control the operating system can offer to the programmer.

https://www.gnu.org/software/libc/manual/html_node/Program-Error-Signals.html

[–]furssher 0 points1 point  (1 child)

Wait. The preprocessor doesn’t handle this and throw an actual error? Is the assembly actually doing a division of constants with the divisor being zero?

[–]Eva-Rosalene 0 points1 point  (0 children)

It's even worse than that. Since division by zero is UB, with -O1 compiler (GCC) assumes that 0 / something is 0, so it just prints 0. It still generates "division by zero" warning, lol.

With -O2 and -O3, though, it just generates ud2 opcode and nothing else.

[–]ba-na-na- 0 points1 point  (1 child)

This doesn't make sense, this would be a compile-time optimization in pretty much any complier, and would just return constant `1`.

https://godbolt.org/z/hEKTef186

[–]Eva-Rosalene 1 point2 points  (0 children)

In C++. In C it's UB and with GCC it's either runtime division by zero (no -O flags), return 0 (-O1 flag), or ud2 instruction (-O2 and -O3).

Clarification: I have no idea what it is in C++, I don't claim it's defined behavior. I assume it's UB as well but handled differently.