Is it ok to have a pointer to an invalid location if you don't use it? [duplicate]

StackOverflow https://stackoverflow.com/questions/19723361

  •  02-07-2022
  •  | 
  •  

Question

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!

Was it helpful?

Solution

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 (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.

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.

OTHER TIPS

If you don't use the pointer again (unless you reassign it) then it's okay.

Just having a pointer doesn't cause undefined behavior, it's when and how you use it that can cause problems. So if you don't use it nothing will happen.

It's fine, but the following is probably clearer and less error prone:

int n, *seq, counter;
scanf("%d", &n);
seq = malloc(n*sizeof(int));
for (counter = 0; counter < n; counter++)
  scanf("%d", &seq[counter]);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top