Aussie AI

memcmp byte comparisons

  • Book Excerpt from "Generative AI in C++"
  • by David Spuler, Ph.D.

memcmp byte comparisons

The memcmp function does a byte-wise comparison of a memory block. Its return value is like strcmp, returning 0 for equality, and a negative or positive value otherwise. However, note that memcmp is not like strcmp, and will not stop when it finds a zero byte.

memcmp return value. A pitfall with memcmp is that you cannot assume that it returns 1 or -1, but must compare the return result to zero (like the strcmp function).

    if (memcmp(&a, &b, sizeof(a)) == 1)  // Bug
    if (memcmp(&a, &b, sizeof(a)) > 0)   // Correct

memcmp object equality testing. Looking at the memcmp function, you might think of it as an opportunity to do a fast equality/inequality test on large objects by simply doing a byte-wise test. You would not be the first to think that.

Unfortunately, there are several obscure pitfalls with this approach. There are multiple ways in C++ that the data in two memory blocks might be identical, but the bytes different:

  • Padding bytes (alignment)
  • Two types of floating-point zero
  • Multiple types of floating-point NaN
  • Bitfield data members (unset padding bits)

You can only use memcmp for memory block comparisons (e.g. tensor equality) if you're sure that these situations cannot occur, such as a contiguous array of primitive data types. Not recommended otherwise.

 

Next:

Up: Table of Contents

Buy: Generative AI in C++: Coding Transformers and LLMs

Generative AI in C++ The new AI programming book by Aussie AI co-founders:
  • AI coding in C++
  • Transformer engine speedups
  • LLM models
  • Phone and desktop AI
  • Code examples
  • Research citations

Get your copy from Amazon: Generative AI in C++