Search code examples
cmemcmp

How to efficiently compare a block of memory to a single byte?


I'm trying to see if a struct came back as all 0xFF for the size of the struct.

memcmp seems like the obvious starting point, BUT I'd have to allocate a second memory block, populate it with 0xFF's. It just seems like a waste.

Does a standard function exist for this? Or should I just punt and iterate via a for loop?


Solution

  • The most obvious solution here seems to be to simply loop over the size of the struct and compare it byte-by-byte.

    The approach of allocating a block of 0xFF followed by memcmp should achieve the same with but a higher space complexity.