Garrick
Garrick

Reputation: 689

Are the following statements using pointer arithmetic involving out of bound aceess valid?

char array[8] = "Raining";

I think all the below comments respective to the statements are true to my understanding.

char *p1 = array + 7;      --> points to '\0'

char *p2 = array + 8;      --> undefined behaviour

char *p3 = array + 9;      --> undefined behaviour

char *p4 = array + 10;     --> undefined behaviour

Is my understanding correct?

Upvotes: 4

Views: 380

Answers (3)

Ziezi
Ziezi

Reputation: 6467

The first line points to a legal element inside the array and the second line is valid until dereferenced. The rest of your post is right.

char *p1 = array + 7;   \\-->points to '\0', within array

char *p2 = array + 8;   \\-->defined behaviour until dereferenced 

The reason the past-the-end addressing is allowed is to make it easy to write a loop to go over an entire array, e.g., while (*d++ = *s++);.

enter image description here

The boundary markers for arrays describe a half-open range, starting with (and including) the first element, and ending with (but not including) the last element: [beginning,end).

Upvotes: 4

Sourav Ghosh
Sourav Ghosh

Reputation: 134376

In your case,

  char *p1 = array + 7;

and

  char *p2 = array + 8;

are valid, because, you are legally allowed to point to

  • any object inside the array length
  • one past the last array element until you dereference it.

OTOH,

  char *p3 = array + 9;

and

  char *p4 = array + 10; 

are undefined.


Quoting C11, chapter §6.5.6, "Additive operators" (emphasis mine)

[same in C99, chapter §6.5.6/p8, for anybody interested]

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. [...]

Upvotes: 15

Bathsheba
Bathsheba

Reputation: 234795

array has 8 elements in it due to the NULL-terminator.

char *p1 = array + 7; is well-defined.

char *p2 = array + 8; is actually defined. You're allowed to set a pointer to one-past the array. (Just don't dereference it.)

The other two are undefined. You're not allowed to set a pointer beyond one-past the array.

Upvotes: 5

Related Questions