Can someone with more knowledge chip in? If I define
void bar(int* const p, int n) {
for (int i=0; i<n; ++i) {
int m = *p
// other code (without UB)
}
}
it might get optimized via loop-invariant code motion to void bar(int* const p, int n) {
int m = *p
for (int i=0; i<n; ++i) {
// other code (without UB)
}
}
so it could still be UB when called with p=NULL and n=0, right?Edit: Oops, in the previous version I used a write instead of a read on *p, making the optimization invalid.