52

Can I compare an int and a size_t variable like this:

int i = 1;
size_t y = 2;
if (i == y) {
    // Do something
}

Or do I have to type cast one of them?

user16217248
  • 3,119
  • 19
  • 19
  • 37

4 Answers4

74

It's safe provided the int is zero or positive. If it's negative, and size_t is of equal or higher rank than int, then the int will be converted to size_t and so its negative value will instead become a positive value. This new positive value is then compared to the size_t value, which may (in a staggeringly unlikely coincidence) give a false positive. To be truly safe (and perhaps overcautious) check that the int is nonnegative first:

/* given int i; size_t s; */
if (i>=0 && i == s)

and to suppress compiler warnings:

if (i>=0 && (size_t)i == s)
Philip Potter
  • 8,975
  • 2
  • 37
  • 47
2

If you're going to compare an int type to size_t (or any other library type), you are doing a risky comparison because int is signed and size_t is unsigned, so one of them will be implicitly converted depending on your compiler/platform. The best thing to do, is to rewrite your int i as:

decltype(y.size()) i = 1;

This assigns your i as the safe type you're trying to compare, and I think it's good practice. This solution is useful in all types of iterators as well. You generally don't want to trust the compiler to cast for you, you can, but it's risky, and an unnecessary risk.

Evg
  • 25,259
  • 5
  • 41
  • 83
Helliarc
  • 21
  • 3
1

size_t is going to be some sort of integer type (although possibly unsigned, so it might generate a warning) so the appropriate casting should be done for you automatically.

As others have already said, you may want to revisit whatever calculation is producing the int and see if you can do it in size_t in the first place if you're computing a required size for something.

Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
  • 13
    `size_t` *must* be unsigned. – Philip Potter Sep 04 '10 at 11:32
  • 3
    Actually unsigned types create more problems than they solve (when used for sizes). A better advice would be to run away from unsigned as a quantity as soon as possible (casting to `int`). You can have problems around `INT_MAX` values, but with unsigned you can have problems around zero and that is a lot more common in programming. – 6502 Aug 29 '17 at 06:34
0

It is okay to compare a size_t value with an int value, the int value will be implicitly converted to unsigned type.

Some compilers will issue a warning when you mix signed and unsigned types in comparisons. In that case you can explicitly convert the signed value to an appropriate unsigned type to suppress the warning.

codaddict
  • 445,704
  • 82
  • 492
  • 529
  • 7
    It's generally not a good idea to just cover up compiler warnings like this, IMO. There are better alternatives (e.g. using one of `size_t` or `int` instead of both in the first place). – strager Sep 04 '10 at 11:27
  • That comparison may cause an `undefined_behaviour` when compiler optimisation is enabled. – anilbey Apr 04 '19 at 09:49