Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decrementing a pointer out of bounds; incrementing it into bounds [duplicate]

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;
}
like image 401
alk Avatar asked Aug 12 '13 12:08

alk


People also ask

What happens if we increment a pointer?

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.

What happens when you increment a pointer in c++?

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.

Can I increment array pointer?

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.

What is pointer out of bounds?

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.


3 Answers

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.

like image 58
Nigel Harper Avatar answered Nov 02 '22 18:11

Nigel Harper


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(where N has the value n) point to, respectively, the i+n-th and i−n-th elements of the array object, provided they exist. Moreover, 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.

like image 45
Yu Hao Avatar answered Nov 02 '22 18:11

Yu Hao


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

like image 31
Shafik Yaghmour Avatar answered Nov 02 '22 18:11

Shafik Yaghmour