Search code examples
c++cmemcmp

Can I pass a null pointer to memcmp?


In particular, is the following well-defined, or does it exhibit undefined behavior?

memcmp(0, 0, 0);

Does this differ between C and C++? Ideally, please provide a quote from the standard(s).


Solution

  • In particular, is the following well-defined, or does it exhibit undefined behavior?

    It's undefined. C99 7.21.1/2 says about all the string functions:

    Unless explicitly stated otherwise in the description of a particular function in this subclause, pointer arguments on such a call shall still have valid values

    and the description of memcmp in 7.21.4.1 doesn't explicitly state otherwise.

    Does this differ between C and C++?

    No, C++ defers to C for its definition of the C library functions, and doesn't have anything special to say about memcmp.