Does the following provoke undefined behavior in line 4 and/or 5:
#include <stdio.h>
int main(void)
{
char s[] = "foo";
char * p = s - 1; /* line 4 */
printf("%s\n", p + 1); /* line 5 */
return 0;
}
Does the following provoke undefined behavior in line 4 and/or 5:
#include <stdio.h>
int main(void)
{
char s[] = "foo";
char * p = s - 1; /* line 4 */
printf("%s\n", p + 1); /* line 5 */
return 0;
}
Decrementing the pointer outside the array bounds is undefined.
C99 standard item 6.5.6 paragraph 8 says, in part,
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 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.
So your line 4 is invoking undefined behaviour since the result is neither within the array or one past the end of it.
Yes the line 4 is undefined behavior!
C99 6.5.6 Additive operators, Section 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.
Does the following provoke undefined behavior in line 4 and/or 5:
Yes, Line 4 is undefined behavior since the pointer is not pointing within the array bounds or one past the array bounds. Although it is valid to point one past the array bounds you can not dereference that element.
The relevant section in the c99 draft standard is 6.5.6
Additive operators paragraph 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 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.
The end of paragraph says that you shall not deference one past the last element:
[...] 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