Kaz Kylheku <643-408-1753@kylheku.com> writes:
On 2025-04-25, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
Thiago Adams <thiago.adams@gmail.com> writes:
Em 4/25/2025 4:05 PM, Keith Thompson escreveu:
Thiago Adams <thiago.adams@gmail.com> writes:
Does anyone know of any platform where integer division by zeroDivision by a constant zero is a constraint violation in a context
returns a number, or in other words, where it's not treated as an
error? I'm asking because division by zero is undefined behaviour, but >>>>>> I think division by a constant zero should be a constraint instead. >>>>>
that requires a constant expression.
Consider this sample
int main(){
int a[1/0];
}
1/0 does not have a value in compile time,
So I believe compilers are making "a" a VLA because 1/0 is
not constant.
1/0 is not a constant expression.
A conforming compiler that supports VLAs (C99, or optionally C11 or
later) would make `a` a VLA, with undefined behavior at runtime when
1/0 is evaluated. For a conforming compiler that doesn't support
VLAs (C89/C90, or optionally C11 or later) the declaration is a
constraint violation.
My interpretation (looking at n3301) is that 1/0 is a constant
expression, which violates a constraint.
("Each constant expression shall evaluate to a constant that is in the
range of representable values for its type.")
The constraint makes it clear that there may be constant expressions
which evaluate out of range. (They are constant expressions in form:
constant operands, subject to the permitted operators.)
The constraint's purpose isn't to give a classifying requirement in the
sense that expressions not meeting the constraint are not taken to be
constant. It is for diagnostic use: constant expressions not meeting the >> constraint are to be diagnosed.
According to this interpretation the declarator a[1/0] would be deemed
to be a regular array, not VLA, and so a constraint violation occurs
regardless of VLA support.
Also looking at n3301, the syntax is:
constant-expression:
conditional-expression
That doesn't imply that all conditional-expressions are
constant-expressions. [...]
Does anyone know of any platform where integer division by zero returns
a number, or in other words, where it's not treated as an error? I'm
asking because division by zero is undefined behaviour, but I think
division by a constant zero should be a constraint instead.
Thiago Adams <thiago.adams@gmail.com> wrote:
Does anyone know of any platform where integer division by zero returns
a number, or in other words, where it's not treated as an error? I'm
asking because division by zero is undefined behaviour, but I think
division by a constant zero should be a constraint instead.
Yeah, I cannot think of an architecture where dividing by zero doesn't
result in an exception or error of some sort. But there's ARM, where it
might return 0, the dividend, or just garbage.
goldside000@outlook.com writes:
Thiago Adams <thiago.adams@gmail.com> wrote:
Does anyone know of any platform where integer division by zero returns >>> a number, or in other words, where it's not treated as an error? I'm
asking because division by zero is undefined behaviour, but I think
division by a constant zero should be a constraint instead.
Yeah, I cannot think of an architecture where dividing by zero doesn't
result in an exception or error of some sort. But there's ARM, where it
might return 0, the dividend, or just garbage.
Yes, I mentioned that in this thread more than a month ago. On two
Linux AARM64 systems I've tried, dividing an integer by zero in a >non-constant expression yields 0.
Sysop: | Tetrazocine |
---|---|
Location: | Melbourne, VIC, Australia |
Users: | 8 |
Nodes: | 8 (0 / 8) |
Uptime: | 103:52:49 |
Calls: | 161 |
Files: | 21,502 |
Messages: | 78,580 |