Search code examples
c++cdtwos-complement

Using -1 as a flag value for unsigned (size_t) types


I was using -1 as a flag value for a function whose return type is size_t (an unsigned type).

I didn't notice it at first, particularly because it wasn't causing any errors in my code (I was checking it with x == -1, not x < 0).

Are there any subtle reasons I shouldn't leave it as is? When might this behave unexpectedly? Is this commonly used?

ptrdiff_t is less common, takes longer to type, and anyway it's not really the appropriate type since the function returns an index into an array.


Solution

  • After trying to think of ways this might go wrong, I realized that there's a danger that the calling function might implicitly cast the return value to a larger type (ie unsigned int to unsigned long long). Then checking if that value == -1 will be false.

    The safer option is to explicitly use size_t.max as the sentinel value. I'm always uncomfortable with changing between signed and unsigned types. Sometimes I think the more reasonable approach is to just make everything signed (like Java does).