Considering the following code, is it safe to do pointer arithmetic on nullptr?
I assume adding any offsets to a nullptr results in another nullptr, so far MSVC produce results as I expected, however I am a bit unsure about whether using nullptr like this is safe:
float * x = nullptr;
float * y = x + 31; // I assume y is a nullptr after this assigment
if (y != nullptr)
{
/* do something */
}
float* a = nullptr; assert(a==nullptr); a++; assert(a!=nullptr);. Adding something tonullptrdoesn't result innullptrwith these compiles. I don't know if it is UB.31. That's not the null pointer. The compiler is allowed to do exactly this. If null pointer + offset was defined to be the null pointer, the compiler would have to add some conditional jump there. Since the committee cares about efficiency, the standard does not mandate what happens when you add a non-zero offset to the null pointer.