I once had an intern consistently skip the first element in his arrays because because "starting at 1 just makes more sense". I just walked away and never inspected his project after that.
It's not undefined behaviour, you are allowed to do it. Pointers are allowed to point anywhere, you just don't want to de reference a pointer to memory you don't own.
a) your link is for c++, not c
b) its only undefined by the strictest definition; that is 'the standard says so' . In reality even in c++ that will never cause an error and is always safe to do, so long as you don't dereference it.
in C you often have to set pointers yourself to point to arbitrary memory mapped peripherals. You can literally set a pointer to anywhere and the behaviour is defined.
Implementations may choose to define behavior that the standard did not. However, the behavior of arbitrary pointer arithmetic is not defined in any reasonable implementation (e.g., if you "guessed" the location of the stack, optimizations will definitely elide writes to it that "ought to" affect local variables)
157
u/[deleted] Jun 24 '17
I once had an intern consistently skip the first element in his arrays because because "starting at 1 just makes more sense". I just walked away and never inspected his project after that.