If I understand correctly, this programme has undefined behavior in C because the intermediate value p 1
is a pointer to uninitialized memory:
int main () {
int x = 0;
int *p = &x;
p = p 1 - 1;
*p = 5;
}
If void
were put in main
's argument list (as required by the C grammar), would it also be undefined behavior in C?
CodePudding user response:
There is neither undefined behavior. You can consider a single object as an array with one element. Using the pointer arithmetic the pointer may point to element past the last element of the array so this statement
p = p 1 - 1;
is correct.
From the C Standard (6.5.6 Additive operators)
7 For the purposes of these operators, a pointer to an object that is not an element of an array behaves the same as a pointer to the first element of an array of length one with the type of the object as its element type.
and
- ...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.
Pay attention to that
- ...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.
CodePudding user response:
I think it's a bit unfortunate that the OP chose p 1 - 1
as an example because p 1
is not undefined behavior as shown in Vlad from Moscow's answer.
The question is more interesting if we consider p 2 - 2
. Here p 2
is indeed undefined behavior. The standard doesn't allow the computation of such a pointer. But does that matter if in the full expression we "undo this computation".
There is an analog for integers. E.g. given i
an integer and if i 2
overflows, thus being Undefined Behavior, is the expression i 2 - 2
ok or Undefined Behavior?
The answer to both is that it is Undefined Behavior. If an expression is Undefined Behavior and the program would reach that expression in its evaluation then the whole program exhibits Undefined Behavior.
There is a more know case about this: computing the mid point of integers: (a b) / 2
is UB if a b
overflows, even if the the final value would fit in the data type.