Sujet : Re: integer divided by zero
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 25. Apr 2025, 22:36:46
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20250425141912.683@kylheku.com>
References : 1 2 3 4
User-Agent : slrn/pre1.0.4-9 (Linux)
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 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.
Division by a constant zero is a constraint violation in a context
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.
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca