undefined behavior - Why 5[a] with C arrays isn't out of range? -
acording post: with arrays, why case a[5] == 5[a]?
where claimed, a[5]
equal 5[a]
.
i'm asking self, true?
while aggree e1[e2] identical (*((e1)+(e2)))
but claim: given declaration of
int a[sizei];
where if (sizei > 5)
has true
i aggree a[5]
ok.
but related cite iso/iec:9899
apendix j.2 undefined behavior:
— array subscript out of range, even if object apparently accessible given subscript (as in lvalue expression
a[1][7]
given declaration inta[4][5]
) (6.5.6).
is (if correct document) 5[a]
"apparently accessible" "array subscript out of range" overall disaggree op linked, a[5]
in given case defined , 5[a]
undefined behaviour.
so, right? or have forgotten consider important?
edit:
my claim doesn't imply
e1[e2] identical (*((e1)+(e2)))
would incorrect.
and 6.5.2.1 array subscripting point 2
just says arithmeticaly identical. nothing said like: (*((e1)+(e2)))
can't achive ub.
so while know section, can't find there thatdestroys claim
also note:
as allready should see in first sentence, i'm asking different question related "problem" stated by
with arrays, why case a[5] == 5[a]?
so please stop marking duplicate of one.
to see why note in appendix j.2 compatible a[b]
being same *(a + b)
, consider how expression a[1][7]
interpreted (assuming declaration int a[4][5]
, in appendix):
a[1][7]
same *(a[1] + 7)
. each element of array a
five-element array, a[1]
evaluates pointer first element of five-element array. if add 7 such pointer, resulting pointer point outside five-element array, meaning not allowed dereference (standards-wise -- might work in practice). that's above expression does.
the note in appendix j.2 has nothing a[5]
vs. 5[a]
way. if array has @ least 6 elements, reasonable call element "apparently accessible" in both of expressions. they're trying clarify you're not allowed access multidimensional array overflow in any of indices, when looks shouldn't overflow boundaries of multidimensional array. it's note -- can derive independently (as above).
Comments
Post a Comment