Here's a simple program to explain what I mean. Given a length n, it initializes a sequence of n integers read by standard input.
int n, *seq, *current;
scanf("%d", &n);
seq = malloc(n*sizeof(int));
for (current = seq; current - seq < n; current++)
scanf("%d", current);
My doubt is about the last increment of current
which makes it point to something outside the allocated memory. Obviously it is an error to read from that location. But what if I don't read that location?
The program runs correctly, but my question is: it is a standard and good practice or is it something that may cause troubles and should be avoided?
UPDATE: And what about appending this to the end of the previous program?
for (current--; current - seq >= 0; current--)
printf("%d\n", *current);
According to @MOHAMED answer it is wrong, not because it is an access to an invalid location, but because the current - seq >= 0
may give wrong results when current
points to one location before the first element, thus having an undefined value!
From this topic:
Yes, a pointer is permitted to point to the location just past the end of the array. However you aren't permitted to deference such a pointer.
C99 6.5.6/8 Additive operators (emphasis added)
if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object, and if the expression Q points one past the last element of an array object, the expression (Q)-1 points to the last element of the array object. If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined. If the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated.
Concerning your UPDATE
And according to this topic:
The comparison current - seq >= 0
is undefined behavior in your case: current - seq
becomes undefined when current
is decremented after the iteration when it is equal to seq
.
This is covered by section 6.5.6 of the standard, part 8:
When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand. If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integer expression. In other words, if the expression
P
points to the i-th element of an array object, the expressions(P)+N
(equivalently,N+(P)
) and(P)-N
(whereN
has the valuen
) point to, respectively, thei+n
-th andi−n
-th elements of the array object, provided they exist. Moreover, if the expressionP
points to the last element of an array object, the expression(P)+1
points one past the last element of the array object, and if the expressionQ
points one past the last element of an array object, the expression(Q)-1
points to the last element of the array object. If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined. If the result points one past the last element of the array object, it shall not be used as the operand of a unary*
operator that is evaluated.
The standard goes length to cover the element at the position one past the last element of the array object, while the element at the position one prior the first element falls under the "otherwise" clause of the above rule.