It's used everywhere in real life C++ code.
quotemstr got a very good point and I'm grateful for the heads up.
C++ is not 100% compatible with C.
------ Old comment:
Makes sense it's undefined behavior, because address 0 is involved. Although I'd guess compiler implementations might just drop whole statement, because length is 0.
Zero length memory copy is a no-op.
Just like static zero iteration loop. They become zero instructions. (Except of course initialization that affects something outside loop scope.)
Edit:
Changed my mind after learning about the tricky UB pathway: If compiler can statically prove memcpy length argument to be zero, it can ALSO prove both argument pointers to be not NULL!
Ouch!
Why handle 0x0 differently?
Not as mechanism that can cause serious bugs, when the compiler can statically prove length argument to be 0 and you can have NULL arguments as input.
Calls to memcpy with zero length are definitely not uncommon. Macros are an obvious way, but it's worse. All you need is a data flow where compiler can statically prove that all paths lead to zero length argument.
I have to agree it's very scary that memcpy(NULL, NULL, 0) is undefined.
Sure about that?
It really should be a no-op. The C and C++ standards are seriously broken in this respect, and compiler authors take advantage of this brokenness to get away with miscompiling programs that, to anyone with common sense, are fine.
The C and C++ standards must be changed to explicitly state that memory operations on zero bytes do nothing regardless of whether the inputs are valid pointers.
It seems so obvious after reading that. Should always ask oneself "what can the compiler prove as a consequence of this undefined behavior?".
So, you're right, memcpy(NULL, NULL, 0) should be defined as a no-op.
Zero-length memcpy pathway to UB is not obvious, because whether the whole thing is UB depends on whether compiler can statically prove the length to be zero. Ugh.
It's also possible you're confusing undefined behavior with implementation defined behavior.