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;
}
When a pointer is incremented, it actually increments by the number equal to the size of the data type for which it is a pointer. For Example: If an integer pointer that stores address 1000 is incremented, then it will increment by 2(size of an int) and the new address it will points to 1002.
The increment ( ++ ) operator increases the value of a pointer by the size of the data object the pointer refers to. For example, if the pointer refers to the second element in an array, the ++ makes the pointer refer to the third element in the array.
Pointers are also useful while working with arrays, because we can use the pointer instead of an index of the array. A pointer can be incremented by value or by address based on the pointer data type.
The pointer dst goes out of bounds when it is computed at the end of the last iteration, and it is never used after that. Besides, it may look like this last value of dst is a one-past-the-end pointer as allowed by the C standard, but that is only true if dst started from 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With